Python Glossary Русский

all Python 3until 3.10collectionsterm

collections.Mapping

removed

Abstract base classes (Mapping, Sequence, Iterable and others) importable straight from collections. Removed in Python 3.10; import them from collections.abc.

Syntax

from collections import Mapping  # до 3.10

Before Python 3.3 the abstract collection classes lived directly in collections. In 3.3 they moved to the collections.abc submodule to separate protocols from concrete types such as deque and Counter, and the old names stayed as aliases; 3.10 removed the aliases. This covers every ABC: Mapping, MutableMapping, Sequence, MutableSequence, Set, MutableSet, Iterable, Iterator, Generator, Callable, Hashable, Sized, Container and others. How to rewrite: from collections import Mapping becomes from collections.abc import Mapping; collections.Sequence becomes collections.abc.Sequence. Concrete classes (OrderedDict, defaultdict, namedtuple) stay in collections.

Examples

# Python 3.10+
try:
    from collections import Mapping
except ImportError as e:
    print(type(e).__name__)  # → ImportError
# Python 3.10+
from collections.abc import Mapping, Sequence, Iterable
print(isinstance({}, Mapping))  # → True
print(isinstance([], Sequence), isinstance('ab', Iterable))  # → True True
from collections import Mapping, Sequence
print(isinstance({}, Mapping), isinstance([], Sequence))  # → True True

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy