immutabledict is a Python library providing an immutable wrapper around dictionaries. It functions as a drop-in replacement for standard dictionaries where immutability is desired, implementing the complete mapping interface. Forked from `frozendict`, `immutabledict` offers an MIT-licensed alternative to the LGPL-3.0 licensed original. It also includes `ImmutableOrderedDict` for order-preserving immutable mappings. The library is actively maintained with frequent minor and patch releases.
pip install immutabledictVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to create an immutabledict and shows that direct modification attempts will result in a TypeError. It also illustrates how to create a new immutabledict based on an existing one with updates.
Instead of `my_immutabledict.copy(key='value')`, use `my_immutabledict | {'key': 'value'}` for simple additions, or `immutabledict({**my_immutabledict, 'key': 'value'})` for more complex updates or Python versions older than 3.9 where the `|` operator is not available for dicts.Upgrade your Python environment to version 3.8 or newer.
To achieve deep immutability, ensure that all values stored within an `immutabledict` are themselves immutable types (e.g., numbers, strings, tuples, or other `immutabledict` instances).
Consider the performance implications for high-frequency update patterns. For scenarios requiring frequent mutable operations before a final 'freeze', a standard `dict` might be more appropriate, converted to `immutabledict` only when immutability is needed for sharing or hashing.
If you maintain custom subclasses that override `__init__` or `__new__`, review their implementation to ensure they correctly chain to `super().__new__` in the base `immutabledict` class for proper initialization.
Instead of modifying in-place, create a new immutabledict with the desired changes. You can use dictionary union (`|`) or recreate the object.
To achieve a similar effect, construct a new immutabledict from the existing one with the desired additions or modifications. For merging, use the dictionary union operator (`|`) or dictionary unpacking.
Before accessing a key, check for its existence using `in` or use the `.get()` method to provide a default value if the key is not found.
Change the import statement to `from immutabledict import immutabledict` or install `frozendict` if that is indeed the intended library.
No dependency data recorded yet.