before you tell what an iterator is, one thing to differentiate: iterable is the difference between iterative and iterator iterators.
There are several kinds of data data types that can be directly scoped for loops:
A class is a collection of data types, such as list, Dict, str, set, tuple, etc.
A class of generator, including generators and generator function with yield
These are objects that can be directly acting on a for loop called an iterative object: iterable
You can use Isinstance () to determine whether an object is an Iterable object
from collections import Iterableprint(isinstance([],Iterable)) ##list列表 #Trueprint(isinstance({},Iterable)) ##dict字典 #Trueprint(isinstance("abc",Iterable)) ##str字符串 #Trueprint(isinstance(1,Iterable)) ##num数字 Falseprint(isinstance((x for x in range(10)),Iterable)) ##加上for循环 True
Returns true to indicate that the Iterable object is a.
The generator generator not only can be scoped for loop, but also can be repeatedly called by the next function and return the next value, knowing that the last throw exception stopiteration error indicates that the next value cannot continue to be returned.
An iterator that can be used for a loop and can also continue to return the next value with Next: Iterator
You can use Isinstance to determine whether an object is an iterator object
from collections import Iteratorprint(isinstance([],Iterator)) ##list列表 #Falseprint(isinstance({},Iterator)) ##dict字典 #Falseprint(isinstance("abc",Iterator)) ##str字符串 #Falseprint(isinstance(1,Iterator)) ##num数字 Falseprint(isinstance((x for x in range(10)),Iterator)) ##加上for循环 True
Generator generator are iterator objects, but list, dict, and Str are iterable, but they are not iterator.
The ITER function can be used to change list, dict, and str to iterable.
from collections import Iteratorprint(isinstance(iter([]),Iterator)) ##list列表 Trueprint(isinstance(iter({}),Iterator)) ##dict字典 Trueprint(isinstance(iter("abc"),Iterator)) ##str字符串 True
You might ask, why are data types such as list, dict, and str not iterator?
This is because the Python iterator object represents a data stream, and the iterator object can be called by the next () function and continuously return to the next data until the Stopiteration error is thrown when there is no data. This data stream can be viewed as an ordered sequence, but we cannot know the length of the sequence in advance, but we will only continue to calculate the next data on demand through the next () function, so the calculation of iterator is inert and is only calculated when the next data needs to be returned.
Iterator 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.
Summarize:
1. Anything that can be scoped for loop is iterable
2. All function objects that can be next are iterator types, which represent a sequence of lazy computations.
3. Collection data Types list, str, dict, and so on are iterable but not iterator, but a iterator object can be obtained through the ITER function.
"26" Python iterator notes