May 2018
Intermediate to advanced
380 pages
9h 37m
English
In Python, an iterator is an object that represents a stream of data. While iterators are available for containers, sequences in particular always support iteration.
Iterators have the __next__() method available (or the built-in next() function). Calling next() multiple times returns successive items from the data stream. When no more items are available, a StopIteration exception is thrown.
Any class can use an iterator by defining a container.__iter__() method. This method returns an iterator object, typically just self. This object is necessary to support the iterator protocol. Different types of iteration can be supported, with each one providing a specific iterator request. For example, a tree structure ...