The concept of iterators and generators has been confusing, summarizing:
Iterators:
1. The so-called iterator is the object with the __next__ () method;
The 2.__iter__ () method returns an Iterator object that must have a __next__ () method;
3. An object that implements the __iter__ () method is iterative, and an object that implements the __next__ () method is an iterator;
4. Built-in function iter () calls the __iter__ () method from an iterative object and returns an Iterator object;
5. The built-in function next () invokes the __next__ () method and advances forward;
The 6.for loop automatically calls the built-in function iter () to obtain an iterator object using the __next__ () method;
7. The file object itself is an iterator, the generator iterator is itself, and the generator automatically supports the iterative protocol;
8. Built-in objects such as lists are not iterators themselves because they support multiple open iterators and need to use the built-in function iter () to start the iterator;
9.__ITER__ () loops only once, and then uses the need to create a new iterator
10. If you want to achieve the effect of multiple iterators, __iter__ () simply defines the new state object for the iterator instead of returning the self
11. If the iterator object is passed to the built-in function iter (), then this function will return the iterator, and if a container class object is passed to the ITER () function, the ITER () function will return a new iterator object each time, which can be used to determine if the input value is the iterator object itself.
Reference: Python Learning Handbook
Python Cookbook
Effective Python
Basic Python Tutorials
"Python" iterator