Skip to main content

Immutable Collections

Project description

An immutable mapping type for Python.

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):



immutables requires Python 3.5+ and is available on PyPI:

$ pip install immutables


immutables.Map is an unordered immutable mapping. Map objects are hashable, comparable, and pickleable.

The Map object implements the ABC so working with it is very similar to working with Python dicts:

import immutables

map = immutables.Map(a=1, b=2)

# will print '1'

print(map.get('z', 100))
# will print '100'

print('z' in map)
# 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:

map2 = map.set('a', 10)
print(map, map2)
# will print:
#   <immutables.Map({'a': 1, 'b': 2})>
#   <immutables.Map({'a': 10, 'b': 2})>

map3 = map2.delete('b')
print(map, map2, map3)
# will print:
#   <immutables.Map({'a': 1, 'b': 2})>
#   <immutables.Map({'a': 10, 'b': 2})>
#   <immutables.Map({'a': 10})>

Maps also implement APIs for bulk updates: MapMutation objects:

map_mutation = map.mutate()
map_mutation['a'] = 100
del map_mutation['b']
map_mutation.set('y', 'y')

map2 = map_mutation.finish()

print(map, map2)
# will print:
#   <immutables.Map({'a': 1, 'b': 2})>
#   <immutables.Map({'a': 100, 'y': 'y'})>

MapMutation objects are context managers. Here’s the above example rewritten in a more idiomatic way:

with map.mutate() as mm:
    mm['a'] = 100
    del mm['b']
    mm.set('y', 'y')
    map2 = mm.finish()

print(map, map2)
# will print:
#   <immutables.Map({'a': 1, 'b': 2})>
#   <immutables.Map({'a': 100, 'y': 'y'})>

Further development

  • An immutable version of Python set type with efficient add() and discard() operations.


Apache 2.0

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for immutables, version 0.9
Filename, size & hash File type Python version Upload date
immutables-0.9-cp35-cp35m-macosx_10_13_x86_64.whl (49.5 kB) View hashes Wheel cp35
immutables-0.9-cp35-cp35m-manylinux1_i686.whl (86.9 kB) View hashes Wheel cp35
immutables-0.9-cp35-cp35m-manylinux1_x86_64.whl (91.4 kB) View hashes Wheel cp35
immutables-0.9-cp35-cp35m-win32.whl (47.7 kB) View hashes Wheel cp35
immutables-0.9-cp35-cp35m-win_amd64.whl (52.4 kB) View hashes Wheel cp35
immutables-0.9-cp36-cp36m-macosx_10_13_x86_64.whl (49.5 kB) View hashes Wheel cp36
immutables-0.9-cp36-cp36m-manylinux1_i686.whl (86.9 kB) View hashes Wheel cp36
immutables-0.9-cp36-cp36m-manylinux1_x86_64.whl (91.6 kB) View hashes Wheel cp36
immutables-0.9-cp36-cp36m-win32.whl (47.7 kB) View hashes Wheel cp36
immutables-0.9-cp36-cp36m-win_amd64.whl (52.3 kB) View hashes Wheel cp36
immutables-0.9-cp37-cp37m-macosx_10_13_x86_64.whl (49.5 kB) View hashes Wheel cp37
immutables-0.9-cp37-cp37m-manylinux1_i686.whl (87.9 kB) View hashes Wheel cp37
immutables-0.9-cp37-cp37m-manylinux1_x86_64.whl (92.5 kB) View hashes Wheel cp37
immutables-0.9-cp37-cp37m-win32.whl (47.7 kB) View hashes Wheel cp37
immutables-0.9-cp37-cp37m-win_amd64.whl (52.4 kB) View hashes Wheel cp37
immutables-0.9.tar.gz (37.8 kB) View hashes Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page