30.6 collections.abc: Built-in ABCs for Containers and Iterables
The collections.abc module provides a rich set of abstract base classes (ABCs) that define the core protocols for Python’s container and iterable types. These ABCs serve as both a formal specification and a mechanism for structural subtyping (duck typing), allowing you to check if an object conforms to a specific interface without requiring explicit inheritance. This is a cornerstone of Python’s philosophy, favoring protocols over rigid type hierarchies. The Core ABCs and Their Hierarchies The ABCs in collections.abc form a sophisticated inheritance hierarchy that mirrors the relationships between different container concepts. At the very top is the Container class, which requires the __contains__ method (in operator support). From there, the hierarchy branches into three main lines: