(Reproduced: http://blog.csdn.net/passionkk/article/details/49929887)
In Python list,truple,str,dict these can be iterated, but they are not iterators. Why?
Because there is a big difference compared to iterators, list/truple/map/dict the size of the data is deterministic, that is to say how many things are known. But the iterator is not, the iterator does not know how many times to execute, so it can be understood to not know how many elements, each call to next (), will go down a step, is inert.
Judging whether it is possible to iterate, with iterable
From collections import iterable
Isinstance ({}, Iterable)-- True
Isinstance ((), iterable)-- True
Isinstance (iterable)--- False
Judging is not an iterator, with iterator
From collections import Iterator
Isinstance ({}, Iterator)--- False
Isinstance ((), Iterator)--- False
Isinstance ((x for x in range), Iterator)- --True
So
Anything that can be used for a loop is iterable.
Anything that can next () is iterator.
The collection data type, such as LIST,TRUPLE,DICT,STR, is itrable not iterator, but a iterator object can be obtained through the ITER () function
The For loop in Python is implemented through next
The difference between iterator and iterable in Python