The `immutables` library for Python provides a high-performance immutable mapping type, `immutables.Map`, built on a Hash Array Mapped Trie (HAMT) data structure. It offers efficient (O(log N)) operations for setting and getting values, making it suitable for functional programming paradigms and scenarios where data integrity and thread safety are paramount. The current version is 0.21. It follows a release cadence driven by bug fixes and performance improvements.
pip install immutablesVerified import paths — ran on the pinned version, not inferred.
Demonstrates creating an `immutables.Map`, performing individual `set` and `delete` operations which return new map instances, and using the `mutate()` context manager for efficient batch updates.
Ensure you understand the specific purpose of `immutables.Map` for immutable dictionary-like structures in Python, rather than general immutable data classes.
Use the provided immutable methods: `new_map = old_map.set(key, value)` to add/update, `new_map = old_map.delete(key)` to remove, or the `with old_map.mutate() as mutable_map:` context manager for batch changes, all of which return a *new* `immutables.Map` instance.
Profile your application with `immutables.Map` for critical sections. For very large, frequently accessed maps where absolute O(1) average time complexity is essential, Python's built-in `dict` or `frozenset` might be more suitable if immutability guarantees are less strict.
Utilize the `with my_map.mutate() as mutable_map:` context manager for batch updates. This approach allows modifications to an intermediate mutable view, committing all changes to a single new immutable `Map` instance at the end of the block, thus optimizing memory and performance.
Ensure the correct library name, 'immutables', is used in the import statement and that the library is installed (`pip install immutables`).
Before accessing a key, check if it exists using `in` or `get()` with a default value. For example: `my_map.get('some_key', default_value)` or `if 'some_key' in my_map: value = my_map['some_key']`.Use immutable types as keys, such as strings, numbers, or tuples. If you need to use a collection as a key, convert it to an immutable type like a tuple: `immutables.Map({(1, 2): 'value'})`.No dependency data recorded yet.