The Module

Learn about the module and mapping abstractions it provides in Python.

One prominent use of abstract base classes is in the module. This module provides the abstract base class definitions for Python’s built-in collections. This is how list, set, and dict (and a few others) can be built from individual component definitions.

We can use the definitions to build our own unique data structures in ways that overlap with built-in structures. We can also use the definitions when writing a type hint for a specific feature of a data structure without being overly specific about alternative implementations that might also be acceptable.

The Mapping abstractions

The definitions in don’t—trivially—include list, set, or dict. Instead, the module provides definitions like MutableSequence, MutableMapping, and MutableSet, which are effectively abstract base classes for which the list, dict, or set classes we use are the concrete implementations. Let’s follow the various aspects of the definition of Mapping back to their origins. Python’s dict class is a concrete implementation of MutableMapping. The abstraction comes from the idea of mapping a key to a value. The MutableMapping class depends on the Mapping definition, an immutable, frozen dictionary potentially optimized for lookups. Let’s follow the relationships among these abstractions.

Here’s the path we want to follow:

Get hands-on with 1200+ tech skills courses.