ABCs of collections
The collections.abc module provides a wealth of abstract base classes that decompose collections into a number of discrete feature sets.
We can successfully use the list class without thinking too deeply about the various features and how they relate to the set class or the dict class. Once we start looking at the ABCs, however, we can see that there's a bit of subtlety to these classes. By decomposing the aspects of each collection, we can see areas of overlapping that manifest themselves as an elegant polymorphism even among different data structures.
At the bottom of the base classes are some "one-trick pony" definitions. These are the base classes that require a single special method:
The
Containerbase class requires the concrete class to implement the__contains__()method. This special method implements theinoperator.The
Iterablebase class requires__iter__(). This special method is used by theforstatement and the generator expressions as well as theiter()function...