The object that can be directly used in the for loop is called iterable object (iterable);
The object that can be called by the next() function and continuously returns the next value is called iterator (iterator);
All iterable objects can be converted into iterators through the built-in function iter().
When using a for loop, the program will automatically call the iterator object of the object to be processed, and then use its next() method until a stoplteration exception is detected.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Because there are no numbers exceeding 0 in the list, when the range exceeds, a StopIteration exception will be returned.
How to judge in a production environment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
The above is the detailed content of Detailed explanation of examples of python iterators. For more information, please follow other related articles on the PHP Chinese website!