We already know that there are several types of data that can directly act on the for Loop:
- A class is a collection of data types, such as,,,
list tuple , and dict set str so on;
One type is generator , including generators and bands yield generator function .
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 collections import iterable>>> isinstance ([], iterable) true >> isinstance ({}, iterable) true > >> isinstance ( ' abc ' , iterable) true >>> isinstance ((x for x Span class= "OP" >in range (10 ), iterable) True >>> isinstance (100 , iterable) false
The generator can not only be used for for loops, but it can also be next() called by the function and return the next value until the last throw StopIteration error indicates that the next value cannot continue to be returned.
An object that can be called by the next () function and continually returns the next value is called an iterator:Iterator
可以使用`isinstance()`判断一个对象是否是`Iterator`对象:>>> from collections import Iterator>>> isinstance((x for x in range(10)), Iterator)True>>> isinstance([], Iterator)False>>> isinstance({}, Iterator)False>>> isinstance(‘abc‘, Iterator)False
Generators are Iterator objects, but,, list dict str Though Iterable they are, they are not Iterator .
To turn, and to list dict str Iterable become Iterator available iter( ) functions:
>>> 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 Python the 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. And list the use is never possible to store all the natural numbers
Summary
- All objects that can be used for a for loop are iterable types;
- All objects that can be used for the next () function 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 .
Pythonforloops 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's basic knowledge iterator