Python Collections ABC

collections.abc is a module that contains abstract base classes.

An abstract base class can be used as a base class for subclassing. It can also be used with isinstance() or issubclass() for duck type checking. It can also be used for type annotations.


AsyncGenerator

A subclass of AsyncIterator. Implements asend() and athrow() methods. Optionally, the __aiter__(), __anext__(), and aclose() methods can be overwritten.


AsyncIterable

Implements an __aiter__() method.


AsyncIterator

A subclass of AsyncIterable. Implements a __anext__() method. Optionally, the aiter()` method can be overwritten.


Awaitable

Implements an __await__() method.

Note that generator-based coroutines (i.e. @types.coroutine()) are awaitable even though they do not have an __await__() method. Using isinstance(obj, collections.abc.Awaitable) for fail to detect them. Instead use inspect.isawaitable().


ByteString

A subclass of Sequence. Implements __len__() and __getitem__() methods. Optionally, the __contains__(), __iter__(), __reversed__(), index(), and count() methods can be overwritten.

In the context of type annotations, ByteString represents the types of bytes, bytearray, and memoryview for byte sequences.

Do not use collections.abc.ByteString for type annotations. The built-in bytes type can be used as a shorthand alias.

b: bytes = b"abc"


Callable

Implements a __call__() method.


Collection

A subclass of Sized, Iterable, and Container. Implements __len__(), __iter__(), and __contains__() methods.


Container

Implements a __contains__() method.


Coroutine

A subclass of Awaitable. Implements send() and throw() methods. Optionally, the close() method can be overwritten.

Note that generator-based coroutines (i.e. @types.coroutine()) are awaitable even though they do not have an __await__() method. Using isinstance(obj, collections.abc.Awaitable) for fail to detect them. Instead use inspect.isawaitable().


Generator

A subclass of Iterator. Implements send() and throw() methods. Optionally, the close(), iter(), and next()` methods can be overwritten.


Hashable

Implements a __hash__() method.


ItemsView

A subclass of MappingView and Set. Optionally, the __contains__() and __iter__() methods can be overwritten.


Iterable

Implements an __iter__() method.

Note that isinstance(obj, collections.abc.Iterable) detects objects that are registered as Iterable or that have an __iter__() method, but it does not detect objects that iterate with the __getitem__() method. The only reliable way to detect an iterable object is iter(obj).


Iterator

A subclass of Iterable. Implements a __next__() method. Optionally, the __iter__() method can be overwritten.


KeysView

A subclass of MappingView and Set. Optionally, the __contains__() and __iter__() methods can be overwritten.


Mapping

A subclass of Collection. Implements __len__(), __iter__(), and __getitem__() methods. Optionally, the __contains__(), keys(), items(), values(), get(), __eq__(), and __ne__() methods can be overwritten.


MappingView

A subclass of Sized. Optionally, the __len__() method can be overwritten.


MutableMapping

A subclass of Mapping. Implements __len__(), __iter__(), __getitem__(), __setitem__(), and __delitem__() methods. Optionally, the __contains__(), keys(), items(), values(), get(), __eq__(), __ne__(), pop(), popitem(), clear(), update(), and setdefault() methods can be overwritten.


MutableSequence

A subclass of Sequence. Implements __len__(), __getitem__(), __setitem__(), __delitem__(), and insert() methods. Optionally, the __contains__(), __iter__(), __reversed__(), index(), count(), append(), reverse(), extend(), pop(), remove(), and __iadd__() methods can be overwritten.


MutableSet

A subclass of Set. Implements __len__(), __iter__(), __contains__(), add(), and discard() methods. Optionally, the __le__(), __lt__(), __eq__(), __ne__(), __gt__(), __ge__(), __and__(), __or__(), __sub__(), __xor__(), isdisjoint(), clear(), pop(), remove(), __ior__(), __iand__(), __ixor__(), and __isub__() methods can be overwritten.


Reversible

A subclass of Iterable. Implements a __reversed__() method.


Set

A subclass of Collection. Implements __len__(), __iter__(), and __contains__() methods. Optionally, the __le__(), __lt__(), __eq__(), __ne__(), __gt__(), __ge__(), __and__(), __or__(), __sub__(), __xor__(), and isdisjoint() methods can be overwritten.


Sequence

A subclass of Collection and Reversible. Implements __len__() and __getitem__() methods. Optionally, the __contains__(), __iter__(), __reversed__(), index(), and count() methods can be overwritten.


Sized

Implements a __len__() method.


ValuesView

A subclass of MappingView and Collection. Optionally, the __contains__() and __iter__() methods can be overwritten.


See also

Python collections.abc module documentation


CategoryRicottone

Python/Collections/Abc (last edited 2023-04-20 14:05:34 by DominicRicottone)