Frozendict is a Python library that provides a simple immutable dictionary implementation, mimicking Python's built-in `dict` but with unchangeable contents after creation. It offers hashability, allowing frozendict instances to be used as keys in other dictionaries or elements in sets. The library is actively maintained, with version 2.4.7 being the latest, and sees regular releases with performance improvements, new features like `deepfreeze`, and support for various architectures.
pip install frozendictVerified import paths — ran on the pinned version, not inferred.
Demonstrates creating a frozendict, accessing elements, its immutability and hashability, and the use of `deepfreeze` for recursive immutability.
If you relied on `deepfreeze` modifying the original object, you must now explicitly assign the result. For example, change `deepfreeze(my_obj)` to `my_obj = deepfreeze(my_obj)` or assign to a new variable.
To ensure deep immutability and hashability when using frozendict with potentially mutable nested data, apply `deepfreeze` to the data before creating the frozendict, or to the frozendict instance after creation. For example, `my_fd = frozendict(deepfreeze(my_dict))` or `my_hashable_fd = deepfreeze(my_fd)`.
Set the environment variable `FROZENDICT_PURE_PY=1` before running your Python script to force the pure Python implementation. This might lead to different performance characteristics.
Upgrade to frozendict v2.4.7 or later to benefit from improved pickling performance when using the C extension.
Instead of modifying an existing `frozendict`, create a new `frozendict` using the `set()` method or the union operator (`|`) to include the desired changes.
To remove an item, create a new `frozendict` using the `delete()` method, which returns a new instance without the specified key.
Ensure that all values within the `frozendict` are themselves immutable and hashable (e.g., numbers, strings, tuples, `frozenset`, or other `frozendict` instances). If you need mutable nested structures, the `frozendict` itself cannot be hashed.
Before accessing an item, check if the key exists using `in` operator, or use the `.get(key, default_value)` method to provide a fallback value if the key is not found.
No dependency data recorded yet.