MultiMapping
raw JSON → 5.1 verified Mon Apr 27 auth: no python
Special MultiMapping objects used in Zope, currently at version 5.1. Provides ordered and unordered mappings supporting multiple values per key (like `NestedMultiMapping` and `IOrderedMultiMapping`). Release cadence is low, with occasional maintenance updates. Requires Python >=3.10.
pip install multimapping Common errors
error ImportError: cannot import name 'MultiMapping' from 'zope.multimapping' ↓
cause The package was renamed to 'multimapping' in version 5.0, so 'zope.multimapping' no longer exists.
fix
Install the new package and update imports: pip install multimapping; from multimapping import MultiMapping
error AttributeError: 'MultiMapping' object has no attribute 'add' ↓
cause The 'add' method was renamed to 'insert' in an older version but then restored. If you see this, you might be using an obsolete version or a different class.
fix
For MultiMapping, use 'add' if available (v5+). For older versions, use 'insert'. Check your version: import multimapping; print(multimapping.__version__)
error TypeError: 'NestedMultiMapping' object does not support item assignment ↓
cause NestedMultiMapping keys must be tuples of arbitrary length. You tried a single key without wrapping it in a tuple.
fix
Use a tuple key: nmm[('key',)] = value, not nmm['key'] = value
Warnings
breaking In version 5.0, the package was renamed from 'zope.multimapping' to just 'multimapping'. All imports must be updated. ↓
fix Replace 'from zope.multimapping import ...' with 'from multimapping import ...'
deprecated The 'IUnorderedMultiMapping' interface is deprecated and will be removed in a future version. Use 'IMultiMapping' instead. ↓
fix Use 'IMultiMapping' from 'multimapping.interfaces'
gotcha Methods like 'getall' return a list, not a tuple. Relying on immutability can break code. ↓
fix If you need an immutable sequence, convert the result to tuple: tuple(mm.getall('key1'))
Imports
- MultiMapping wrong
from zope.multimapping import MultiMappingcorrectfrom multimapping import MultiMapping - NestedMultiMapping
from multimapping import NestedMultiMapping - IOrderedMultiMapping
from multimapping import IOrderedMultiMapping
Quickstart
from multimapping import MultiMapping, NestedMultiMapping
# Create a simple MultiMapping
mm = MultiMapping()
mm['key1'] = 'value1'
mm.add('key1', 'value2')
print(mm.getall('key1')) # ['value1', 'value2']
# NestedMultiMapping stores keys as ordered tuples
nmm = NestedMultiMapping()
nmm[('a', 'b')] = 1
print(nmm[('a', 'b')]) # 1