Python tutorial 7 -- Python iteratable object: Iterable, Iterator: Iterator
We already know that the data types that can directly act on the for loop are as follows:
The first type is the set data type, such as list/tuple/dict/set/str;
The first type is generator, which includes generator and generator function with yield.
These objects that can directly act on the for loop are collectively referred to as iteration objects: Iterable.
You can use isinstance () to 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 in range(10)), Iterable)True>>> isinstance(100, Iterable)False
The generator can not only act on the for loop, but also be continuously called by the next () function and return the next value until the StopIteration error is thrown to indicate that the next value cannot be returned.
The object that can be called by the next () function and continuously return the next value is called the Iterator: Iterator.
You can use isinstance () to determine whether an object is an Iterator object:
>>> from collections import Iterator>>> isinstance((x for x in range(10)),Iterator)True>>> isinstance([],Iterator)False>>> isinstance({},Iterator)False>>> isinstance('abc',Iterator)False
All generators are Iterator objects, but although list/dict/str is Iterable, It is not IteratZ generator? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcqGjPC9wPg0KPHA + fuse/J0tTKudPDaXRlcigpuq/K/aO6PC9wPg0KPHByZSBjbGFzcz0 =" brush: java;>>> isinstance(iter([]),Iterator)True>>> isinstance(iter('abc'),Iterator)True
You may ask why data types such as list, dict, and str are not Iterator?
This is because the Iterator object in Python represents a data stream. The Iterator object can be called by the next () function and continuously return the next data until the StopIteration error is thrown when no data exists. We can regard this data stream as an ordered sequence, but we cannot know the sequence length in advance. We can only use the next () function to calculate the next data on demand, therefore, the computation of Iterator is inert and will be calculated only when the next data needs to be returned.
Iterator can even represent an infinitely large data stream, such as all natural numbers. However, using list is never possible to store all natural numbers.
Summary
All objects that can act on the for loop are of the Iterable type;
All objects that can act on the next () function are of the Iterator type. They represent a sequence of inert computing;
Set data types such as list/dict/str/are Iterable iteratable objects, but they are not Iterator iterators. However, you can use the iter () function to obtain an Iterator object.
Python's for loop is implemented by constantly calling the next () function, for example:
>>> for x in [1,2,3,4,5]:... pass...
In fact, it is equivalent:
# First obtain the Iterator object: it = iter ([1, 2, 3, 4, 5]) # loop while True: try: # Get the next value: x = next (it) Stopping t StopIteration: # exit the loop break when StopIteration is encountered