A simple immutable dictionary implementation with hashing support and performance optimizations
npx @tessl/cli install tessl/pypi-frozendict@2.4.0A simple immutable dictionary implementation for Python that maintains the same API as the built-in dict type while guaranteeing immutability. It offers fast operations (often faster than dict), supports hashing when all values are hashable, includes pickle/unpickle functionality, and provides additional methods for creating new frozendict instances.
pip install frozendictfrom frozendict import frozendictWith additional features:
from frozendict import frozendict, deepfreeze, FrozendictJsonEncoderFull feature import:
from frozendict import (
frozendict,
deepfreeze,
register,
unregister,
FrozendictJsonEncoder,
c_ext,
__version__
)Submodule access:
import frozendict.monkeypatch
import frozendict.coolfrom frozendict import frozendict
# Create a frozendict from a regular dict
data = frozendict({'name': 'Alice', 'age': 30, 'city': 'New York'})
# Access values like a regular dict
print(data['name']) # Alice
print(data.get('age', 0)) # 30
# frozendict is immutable - these would raise AttributeError:
# data['name'] = 'Bob' # AttributeError: 'frozendict' object is read-only
# del data['age'] # AttributeError: 'frozendict' object is read-only
# Create new frozendict instances with modifications
updated = data.set('age', 31)
print(updated['age']) # 31
print(data['age']) # 30 (original unchanged)
# Remove items
smaller = data.delete('city')
print('city' in smaller) # False
print('city' in data) # True (original unchanged)
# frozendict is hashable (if all values are hashable)
my_set = {data, updated}
print(len(my_set)) # 2
# Use as dictionary keys
cache = {data: "cached_result"}frozendict provides both pure Python and C extension implementations:
The library automatically registers frozendict with collections.abc.Mapping and applies JSON serialization patches on import.
The main frozendict class providing an immutable dictionary with dict-compatible API, hashing support, and methods for creating modified copies.
class frozendict(dict):
def __init__(*args, **kwargs): ...
def __hash__(self): ...
def set(self, key, val): ...
def delete(self, key): ...
def setdefault(self, key, default=None): ...
@classmethod
def fromkeys(cls, *args, **kwargs): ...Recursive immutability system that converts objects and all nested objects to their immutable counterparts, with support for custom type converters.
def deepfreeze(o, custom_converters=None, custom_inverse_converters=None): ...
def register(to_convert, converter, *, inverse=False): ...
def unregister(type, inverse=False): ...
def getFreezeConversionMap(): ...
def getFreezeConversionInverseMap(): ...Automatic JSON serialization support and monkeypatch utilities for seamless integration with JSON libraries.
class FrozendictJsonEncoder(JSONEncoder):
def default(self, obj): ...
def patchOrUnpatchJson(*, patch, warn=True): ...
def patchOrUnpatchOrjson(*, patch, warn=True): ...
def patchOrUnpatchAll(*, patch, warn=True, raise_orjson=False): ...c_ext: bool # True if C extension is used, False for pure Python
__version__: str # Package version stringclass FreezeError(Exception): ...
class FreezeWarning(Warning): ...
class MonkeypatchWarning(Warning): ...FrozenOrderedDict = frozendict # Deprecated alias, to be removed in future versions