June 2019
Beginner to intermediate
770 pages
19h 24m
English
Iterator objects are created implicitly when we use an iterable container with a for statement. We rarely expect to see the iterator object itself. For the most part, it will be a concealed portion of the implementation of the for statement. The few times we do care about the iterator object, we rarely want to extend or revise the class definition.
We can expose the implicit iterators that Python uses through the iter() function. We can interact with an iterator in the following way:
>>> x = [1, 2, 3] >>> iter(x) <list_iterator object at 0x1006e3c50> >>> x_iter = iter(x) >>> next(x_iter) 1 >>> next(x_iter) 2 >>> next(x_iter) 3 >>> next(x_iter) Traceback (most recent call last): File "<stdin>", line 1, in <module> ...
Read now
Unlock full access