June 2019
Beginner to intermediate
770 pages
19h 24m
English
When our design involves wrapping an existing class, we'll need to be sure our class is iterable. When we look at the documentation for collections.abc.Iterable, we see that we only need to define __iter__() to make an object iterable. The __iter__() method can either return a proper Iterator object, or it can be a generator function.
Creating an Iterator object, while not terribly complex, is rarely necessary. It's much simpler to create generator functions. For a wrapped collection, we should always simply delegate the __iter__() method to the underlying collection.
For our StatsList3 class, it would look like this:
def __iter__(self):
return iter(self._list)
This method function would delegate the iteration ...
Read now
Unlock full access