The ABCs of abstract base classes
In Chapter 6, More Complex Data Types, we looked at the collections
module, which offers a number of variations on the mapping theme. These different kinds of collections are built on a foundation of abstract base classes, defined in the collections.abc
module. Looking at this module exposes the common features, and the differences, among the collections.
We can see how Sequence
is the basis for the built-in tuple class
, and MutableSequence
is the basis for the built-in list
. The Set
abstract base class is the basis for the frozenset
built-in class, and MutableSet
is the basis for the set
class. There's no concrete implementation of the Mapping
class, but the dict
class is the built-in implementation of the MutableMapping
class.
If we need to implement a unique kind of collection, one not already provided by the collection
module, we're encouraged to use the collections.abc
module as a starting point. If we leverage these common base classes, we...