Python collections.abc: test container interfaces with ABCs
Answer: This module provides abstract base classes used to test whether a class provides a particular interface. Examples include hashable types and mappings. Existing classes can be registered as virtual subclasses of the ABCs.
Testing an interface
Abstract base classes in this module can be used to test whether a class provides a particular interface.
Examples include whether a type is hashable, and whether it is a mapping.
A newly written class can inherit directly from one of these abstract base classes.
Added in version 3.3: formerly, this module was part of the collections module.
Virtual subclasses
Existing classes and built-in classes can be registered as virtual subclasses of the ABCs.
Users can then rely on issubclass or isinstance tests to determine whether the full interface is supported.
Registered classes should define the full API, including the abstract methods and the mixin methods.
Methods that are automatically inferred from the rest of the API are the exception to this rule.
See also
Claims
- id: C1 claim: The ABCs test whether a class provides a particular interface. source: S1 evidence: "test whether a class provides a particular interface"
- id: C2 claim: Examples include whether a type is hashable or a mapping. source: S1 evidence: "whether it is hashable or whether it is a mapping"
- id: C3 claim: Existing classes can be registered as virtual subclasses of the ABCs. source: S2 evidence: "registered as \"virtual subclasses\" of the ABCs"
- id: C4 claim: Registered classes should define the full API. source: S2 evidence: "define the full API including all of the abstract methods"
Sources
- [S1] collections.abc — Abstract Base Classes for Containers. Python Software Foundation. https://docs.python.org/3/library/collections.abc.html . License: PSF License Version 2. Accessed 2026-11-23.
- [S2] collections.abc documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/collections.abc.rst . License: PSF License Version 2. Accessed 2026-11-23.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, collections.abc — Abstract Base Classes for Containers; CPython, collections.abc documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, collections.abc — Abstract Base Classes for Containers; CPython, collections.abc documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python collections.abc: test container interfaces with ABCs." iinventedlight Library, iil:note:c2xk2nv1, version of 2026-11-23. https://iinventedlight.com/library/notes/python-collections-abc/. License: CC-BY-4.0.
@misc{iil_note_c2xk2nv1,
author = {{iinventedlight}},
title = {{Python collections.abc: test container interfaces with ABCs}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-23},
url = {https://iinventedlight.com/library/notes/python-collections-abc/},
note = {Permanent ID iil:note:c2xk2nv1. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-collections-abc.schema.json (schema; CC-BY-4.0)
- python-collections-abc.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-collections-abc.md.