iterator = loop
Iterators
We already know that for there are several types of data that can be directly acting on a loop:
A class is a collection of data types, such as,,, list tuple , and dict set str so on;
One is generator to include the generator and yield the generator function with the band.
These objects, which can be directly applied to for the loop, are called iterative objects: Iterable .
You can use to isinstance() determine whether an object is an Iterable object:
>>>From collectionsImport iterable>>> isinstance([], iterable)True>>> isinstance({}, iterable)True>>> isinstance(' ABC ') true< Span class= "token operator" >>>> isinstance ( (x for x in range10true>>> isinstance< Span class= "token punctuation" > (100 Iterablefalse
The generator can not only be used for a for loop, but it can also be called by the next () function and return the next value until the last throw Stopiteration error indicates that the next value cannot be returned again.
An object that can be called by the next () function and continually returns the next value is called an iterator: Iterator.
You can use Isinstance () to determine whether an object is a iterator object:
>>>From collectionsImport Iterator>>> isinstance((xFor XIn range(10)), Iterator)True>>> isinstance ([]false>>> isinstance{}" false>> > isinstance ( ' abc ') Span class= "token punctuation", Iteratorfalse
Generators are Iterator objects, but,, list dict str Though Iterable they are, they are not Iterator .
Turn list , dict and str wait for the Iterable Iterator function to be used iter() :
>>> isinstance(iter([]), Iterator)True>>> isinstance(iter(‘abc‘), Iterator)True
You may ask, why, list dict , str etc. data types are not Iterator ?
This is because the Python Iterator object represents a data stream, and the iterator object can be next() called by the function and will return the next data continuously until there is no data to throw an StopIteration error. You can think of this data stream as an ordered sequence, but we can't know the length of the sequence in advance, only by continuously using the next() function to calculate the next data on demand, so Iterator the calculation is lazy, and it will only be calculated when the next data needs to be returned.
IteratorIt can even represent an infinitely large stream of data, such as the whole natural number. Using list is never possible to store all natural numbers.
Summary
Any object that can be used for for the loop is a Iterable type;
All objects that can be used for next() functions are Iterator types, which represent a sequence of lazy computations;
Collection data types such as list , dict ,, and str so on are Iterable not Iterator , however, you can iter() get an object from a function Iterator .
Python3 for loops are essentially implemented by calling next() functions, for example:
for x in [1, 2, 3, 4, 5]: pass
is actually exactly equivalent to:
# 首先获得Iterator对象:it = iter([1, 2, 3, 4, 5])# 循环:while True: try: # 获得下一个值: x = next(it) except StopIteration: # 遇到StopIteration就退出循环 break
Python Basic -2.4.3 iterator