The term "mapping" is described in the Python glossary as:
A container object that supports arbitrary key lookups and implements
the methods specified in the Mapping
or MutableMapping
abstract base
classes. Examples include dict
, collections.defaultdict
,
collections.OrderedDict
and collections.Counter
.
The requirements to subclass collections.abc.Mapping
are described in its docstring:
"""A Mapping is a generic container for associating key/value
pairs.
This class provides concrete generic implementations of all
methods except for __getitem__, __iter__, and __len__.
"""
So you can define a new mapping type by subclassing collections.abc.Mapping
, and implementing three methods: __len__
, __getitem__
, and __iter__
.
>>> from collections.abc import Mapping
>>> def func(**kwargs):
... print(kwargs)
...
>>> class MyMapping(Mapping):
... def __len__(self):
... return 1
... def __getitem__(self, k):
... return 'bananas'
... def __iter__(self):
... return iter(['custard'])
...
>>> func(**MyMapping())
{'custard': 'bananas'}