The internal implementation principle of for...in... in python

tags: python  principle  for

The for loop traversal is actually to take out the iterator in the iterable object and then perform the next() operation on the iterator, and then deal with the exception thrown when the iterator next() was last time.

Below we use a while to simulate the implementation of for...in...

lists = [i * 2 for i in range(5)]
for temp in lists:
    print(temp, end='')

print('\r\nThe following is the output of using while to simulate for...in...')

iterator_ = iter(lists)
while True:
    try:
        print(next(iterator_), end='')
    except StopIteration as ret:
        # print(ret)
        break

The output result is

02468
 The following is the output of using while to simulate for...in...
02468



Intelligent Recommendation

ReentrantLock internal principle implementation

REENTRANTLOCK is implemented based on AQS (ABSTRACTQUEEDSYNCHRONIZER). It is divided into fair locks and non -fair locks. The ReentrantLock can be set to a fair lock by entering and out of True in the...

SDWebImage internal implementation principle steps

The entry setImageWithUrl:placeHolderImage:options: will display the placeHolderImage, and then the SDWebImageManager will start processing the image based on the URL. Enter SDWebImageManager-download...

NSDictionary internal structure, implementation principle

I don't know if you have thought about how NSDictionary and NSArray are implemented internally, so dig into the internal structure of NSDictionary & NSArray today. First let's take a look at these...

The essence of Epoll (internal implementation principle)

In the server-side development, it is indispensable to contact network programming. Epoll is essential for high-performance web servers under Linux, and multiplex technology is used by nginx, redis, s...

Internal implementation principle of IOS SDWebImage

Everyone must be familiar with SDWebImage, and it is often used in projects. Can you know the principle of its realization? Share it with you today. First look at the following picture: Picture explan...

More Recommendation

InnoDB internal implementation principle analysis

The content of this article comes from the technical sharing within the team. It mainly introduces the internal implementation principles of InnoDB. Based on the official documents and some PPT introd...

Semaphore (semaphore) internal implementation principle

 Semaphore is used to manage the semaphore. In concurrent programming, you can control the number of threads that access the synchronization code. Semaphore passes in an int value when instantiating, ...

Discussion on the principle of internal implementation of mysql

MySQL index principle Disk pre-reading principle The computing and processing data in the computer rely on the CPU, which is the central processor, which is generally divided into several levels in th...

Internal implementation principle called in JVM

This article is "in-depth understanding of Java virtual machine" learning notes # and direct reference First come and understandSymbol referencewithDirect referencethe concept of: The Java c...

Ribbon principle and internal algorithm implementation

    1、Randomrule strategy (com.netflix.loadbalancer.randomrule) Randomrule Policy: This policy implements from the list of service instancesrandom selectionA service instance as a request se...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top