You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The underlying datastructure is a Hash Array Mapped Trie (HAMT)
used in Clojure, Scala, Haskell, and other functional languages.
This implementation is used in CPython 3.7 in the contextvars
module (see PEP 550 and
PEP 567 for more details).
Immutable mappings based on HAMT have O(log N) performance for both
set() and get() operations, which is essentially O(1) for
relatively small mappings.
Below is a visualization of a simple get/set benchmark comparing
HAMT to an immutable mapping implemented with a Python dict
copy-on-write approach (the benchmark code is available
here):
Installation
immutables requires Python 3.6+ and is available on PyPI:
$ pip install immutables
API
immutables.Map is an unordered immutable mapping. Map objects
are hashable, comparable, and pickleable.
The Map object implements the collections.abc.Mapping ABC
so working with it is very similar to working with Python dicts:
importimmutablesmap=immutables.Map(a=1, b=2)
print(map['a'])
# will print '1'print(map.get('z', 100))
# will print '100'print('z'inmap)
# will print 'False'
Since Maps are immutable, there is a special API for mutations that
allow apply changes to the Map object and create new (derived) Maps: