Skip to main content
https://travis-ci.org/MagicStack/immutables.svg?branch=master https://ci.appveyor.com/api/projects/status/tgbc6tq56u63qqhf?svg=true https://img.shields.io/pypi/v/immutables.svg

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

bench.png

Installation

immutables requires Python 3.5+ 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:

import immutables

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

print(map['a'])
# 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.

License

Apache 2.0

Metadata

Release files for immutables 0.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for immutables 0.11
File Size Uploaded
immutables-0.11.tar.gz 38.6 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for immutables 0.11
File
immutables-0.11-cp37-cp37m-win_amd64.whl CPython 3.7 CPython 3.7 pymalloc Windows x86-64 Details
immutables-0.11-cp37-cp37m-win32.whl CPython 3.7 CPython 3.7 pymalloc Windows x86-32 Details
immutables-0.11-cp37-cp37m-manylinux1_x86_64.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.5+ x86-64 Details
immutables-0.11-cp37-cp37m-manylinux1_i686.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.5+ x86-32 Details
immutables-0.11-cp37-cp37m-macosx_10_13_x86_64.whl CPython 3.7 CPython 3.7 pymalloc macOS 10.13+ x86-64 Details
immutables-0.11-cp36-cp36m-win_amd64.whl CPython 3.6 CPython 3.6 pymalloc Windows x86-64 Details
immutables-0.11-cp36-cp36m-win32.whl CPython 3.6 CPython 3.6 pymalloc Windows x86-32 Details
immutables-0.11-cp36-cp36m-manylinux1_x86_64.whl CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.5+ x86-64 Details
immutables-0.11-cp36-cp36m-manylinux1_i686.whl CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.5+ x86-32 Details
immutables-0.11-cp36-cp36m-macosx_10_13_x86_64.whl CPython 3.6 CPython 3.6 pymalloc macOS 10.13+ x86-64 Details
immutables-0.11-cp35-cp35m-win_amd64.whl CPython 3.5 CPython 3.5 pymalloc Windows x86-64 Details
immutables-0.11-cp35-cp35m-win32.whl CPython 3.5 CPython 3.5 pymalloc Windows x86-32 Details
immutables-0.11-cp35-cp35m-manylinux1_x86_64.whl CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.5+ x86-64 Details
immutables-0.11-cp35-cp35m-manylinux1_i686.whl CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.5+ x86-32 Details
immutables-0.11-cp35-cp35m-macosx_10_13_x86_64.whl CPython 3.5 CPython 3.5 pymalloc macOS 10.13+ x86-64 Details

Total release size: 1.0 MB

Release files / immutables-0.11.tar.gz

Download URL immutables-0.11.tar.gz
Size 38.6 kB
Tags Source
SHA-256 checksum
How to use checksums
d6850578a0dc6530ac19113cfe4ddc13903df635212d498f176fe601a8a5a4a3
BLAKE2b-256 checksum
How to use checksums
3934d5f6797f9b835d755d047d7a592a0f2f0044f82b5c9a184a5fbe7488808f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3

Release files / immutables-0.11-cp37-cp37m-win_amd64.whl

Download URL immutables-0.11-cp37-cp37m-win_amd64.whl
Size 53.4 kB
Tags CPython 3.7 CPython 3.7 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
6b6d8d035e5888baad3db61dfb167476838a63afccecd927c365f228bb55754c
BLAKE2b-256 checksum
How to use checksums
c41a181fb128db26a443510c3af85c834f5e9ce583e8493b8967546c109c6c43
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3

Release files / immutables-0.11-cp37-cp37m-win32.whl

Download URL immutables-0.11-cp37-cp37m-win32.whl
Size 48.8 kB
Tags CPython 3.7 CPython 3.7 pymalloc Windows x86-32
SHA-256 checksum
How to use checksums
545186faab9237c102b8bcffd36d71f0b382174c93c501e061de239753cff694
BLAKE2b-256 checksum
How to use checksums
a33ba773834eec3d14b58826fa24434052411d80355dcfc8d512cd42adce1544
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3

Release files / immutables-0.11-cp37-cp37m-manylinux1_x86_64.whl

Download URL immutables-0.11-cp37-cp37m-manylinux1_x86_64.whl
Size 93.7 kB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
1c2e729aab250be0de0c13fa833241a778b51390ee2650e0457d1e45b318c441
BLAKE2b-256 checksum
How to use checksums
b2bd5d3476a4f3483b4802b48f2040e62c23e43f2315a74977769b76293f754f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3

Release files / immutables-0.11-cp37-cp37m-manylinux1_i686.whl

Download URL immutables-0.11-cp37-cp37m-manylinux1_i686.whl
Size 89.0 kB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.5+ x86-32
SHA-256 checksum
How to use checksums
2c536ff2bafeeff9a7865ea10a17a50f90b80b585e31396c349e8f57b0075bd4
BLAKE2b-256 checksum
How to use checksums
9860d32c5e9c7edaafbaa8e105d687b16dcda1030acc97447386ae9cb16b7fd3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3

Release files / immutables-0.11-cp37-cp37m-macosx_10_13_x86_64.whl

Download URL immutables-0.11-cp37-cp37m-macosx_10_13_x86_64.whl
Size 50.6 kB
Tags CPython 3.7 CPython 3.7 pymalloc macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
518f20945c1f600b618fb691922c2ab43b193f04dd2d4d2823220d0202014670
BLAKE2b-256 checksum
How to use checksums
5f5b53e1b2a9329fcaa385428bf666c998686f50ff606ae5eb0270254fed2032
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3

Release files / immutables-0.11-cp36-cp36m-win_amd64.whl

Download URL immutables-0.11-cp36-cp36m-win_amd64.whl
Size 53.4 kB
Tags CPython 3.6 CPython 3.6 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
b8fed714f1c84a3242c7184838f5e9889139a22bbdd701a182b7fdc237ca3cbb
BLAKE2b-256 checksum
How to use checksums
9f5051289aef23562beef9005220658725b236ff3f0fe6bfa36246fbc6a95a9a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3

Release files / immutables-0.11-cp36-cp36m-win32.whl

Download URL immutables-0.11-cp36-cp36m-win32.whl
Size 48.8 kB
Tags CPython 3.6 CPython 3.6 pymalloc Windows x86-32
SHA-256 checksum
How to use checksums
0f07c58122e1ce70a7165e68e18e795ac5fe94d7fee3e045ffcf6432602026df
BLAKE2b-256 checksum
How to use checksums
7f915ae4e8b87d0c930512aae39bc1b41aed1c7a7ebfcf73a597b0311d1ae0b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3

Release files / immutables-0.11-cp36-cp36m-manylinux1_x86_64.whl

Download URL immutables-0.11-cp36-cp36m-manylinux1_x86_64.whl
Size 92.7 kB
Tags CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
b268422a5802fbf934152b835329ac0d23b80b558eaee68034d45718edab4a11
BLAKE2b-256 checksum
How to use checksums
62cc3961b18a1a689a7e3232d923a8546cb901c49b4a33be6987c03a89175c4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3

Release files / immutables-0.11-cp36-cp36m-manylinux1_i686.whl

Download URL immutables-0.11-cp36-cp36m-manylinux1_i686.whl
Size 88.0 kB
Tags CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.5+ x86-32
SHA-256 checksum
How to use checksums
f5b93248552c9e7198558776da21c9157d3f70649905d7fdc083c2ab2fbc6088
BLAKE2b-256 checksum
How to use checksums
fdb74cfbaac47fde635b93bfb14c3232e695f9793abfc83e0a47a64be3383bc1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3

Release files / immutables-0.11-cp36-cp36m-macosx_10_13_x86_64.whl

Download URL immutables-0.11-cp36-cp36m-macosx_10_13_x86_64.whl
Size 50.6 kB
Tags CPython 3.6 CPython 3.6 pymalloc macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
422c7d4c75c88057c625e32992248329507bca180b48cfb702b4ef608f581b50
BLAKE2b-256 checksum
How to use checksums
97904ad8d6f5c63281004a871b6fbc8ca1507f86d681b4aec41fac415da45ee6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3

Release files / immutables-0.11-cp35-cp35m-win_amd64.whl

Download URL immutables-0.11-cp35-cp35m-win_amd64.whl
Size 53.4 kB
Tags CPython 3.5 CPython 3.5 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
0aa055c745510238cbad2f1f709a37a1c9e30a38594de3b385e9876c48a25633
BLAKE2b-256 checksum
How to use checksums
fdf5ee265d5976498e0505b056cdf57006b17bca36185f24f85130d18a38e0eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3

Release files / immutables-0.11-cp35-cp35m-win32.whl

Download URL immutables-0.11-cp35-cp35m-win32.whl
Size 48.8 kB
Tags CPython 3.5 CPython 3.5 pymalloc Windows x86-32
SHA-256 checksum
How to use checksums
e87bd941cb4dfa35f16e1ff4b2d99a2931452dcc9cfd788dc8fe513f3d38551e
BLAKE2b-256 checksum
How to use checksums
40f57e611152c0beacbff9b48562cab2ead878977a9fee514013c63a7970b861
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3

Release files / immutables-0.11-cp35-cp35m-manylinux1_x86_64.whl

Download URL immutables-0.11-cp35-cp35m-manylinux1_x86_64.whl
Size 92.6 kB
Tags CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
2de2ec8dde1ca154f811776a8cbbeaea515c3b226c26036eab6484530eea28e0
BLAKE2b-256 checksum
How to use checksums
481d9e457d5cccafe976e68c80b161538a8bee8ca1f407be5627877b3123a6c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3

Release files / immutables-0.11-cp35-cp35m-manylinux1_i686.whl

Download URL immutables-0.11-cp35-cp35m-manylinux1_i686.whl
Size 87.9 kB
Tags CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.5+ x86-32
SHA-256 checksum
How to use checksums
c7eb2d15c35c73bb168c002c6ea145b65f40131e10dede54b39db0b72849b280
BLAKE2b-256 checksum
How to use checksums
3c2d4b248b111e9e2d6a210a709bd09936932bb72dc35b2b9550befe7740e568
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3

Release files / immutables-0.11-cp35-cp35m-macosx_10_13_x86_64.whl

Download URL immutables-0.11-cp35-cp35m-macosx_10_13_x86_64.whl
Size 50.6 kB
Tags CPython 3.5 CPython 3.5 pymalloc macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
bce27277a2fe91509cca69181971ab509c2ee862e8b37b09f26b64f90e8fe8fb
BLAKE2b-256 checksum
How to use checksums
477412cbd7dac0e45896b1711533fc4e53728d664367eaffe159241c004e0522
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.3

Release history Release notifications | RSS feed

0.21

49 release files

0.20

41 release files

0.19

41 release files

0.18

49 release files

0.17

49 release files

0.16

27 release files

0.15

15 release files

0.14

12 release files

0.13

12 release files

0.12

12 release files

This release

0.11 This release

16 release files

0.10

16 release files

0.9

16 release files

0.8

16 release files

0.7

16 release files

0.6

11 release files

0.5

11 release files

0.4

11 release files

0.3

11 release files

0.2

11 release files

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page