Python 3.3+collectionsterm
collections.abc.Mapping
collections.abc — protocols
The immutable mapping protocol (key access, len, in).
Syntax
isinstance(obj, collections.abc.Mapping)
Immutable here describes the protocol, not the object: a plain dict passes this check too, so it does not guarantee that nobody can mutate what you were handed — require MutableMapping when your code needs to write. The upside is inheritance: subclass Mapping and implement just __getitem__, __len__ and __iter__, and you get get, keys, items, values, membership tests and equality for free.
Examples
import collections.abc as abc
import types
print(isinstance({}, abc.Mapping)) # → True
print(isinstance([1, 2], abc.Mapping)) # → False
print(isinstance({}, abc.MutableMapping)) # → True
print(isinstance(types.MappingProxyType({'a': 1}), abc.Mapping)) # → True
print(isinstance(types.MappingProxyType({'a': 1}), abc.MutableMapping)) # → False
See also
Open in the full glossarydocs.python.orgReport an inaccuracy