10.2 Iterators
We can think of an iterator as an object with a next() method and a done() method; the latter returns True if there are no items left in the sequence. In a programming language without built-in support for iterators, the iterator would be used like this:
iterator = some_collection.iterator()
while not iterator.done():
item = iterator.next()
# do something with the item from some_collection...
This example omits the class for some_collection, which needs to implement an iterator() method to return a stateful object to iterate through items in the collection. It also omits the class for an iterator, which is initialized with the value of some_collection and then handles the next() and done() methods to return each item.
In Python, iteration is available across many language features, so the method...