Freeze introduces 3 frozen collections: FDict
, FSet
and FList
.
They are immutable, hashable, support type-hinting, and will attempt to recursively convert mutable
collections into frozen counterparts on initialization.
While there are built-in immutable versions for list (tuple) and set (frozenset), they have some issues:
- tuple can store mutable objects so its immutability is not guaranteed.
- frozenset can't be initialized with mutable objects.
- dict doesn't have a built-in immutable version at all.
pip install frz
from freeze import FDict, FList, FSet
a_mutable_dict = {
"list": [1, 2],
"set": {3, 4},
}
a_frozen_dict = FDict(a_mutable_dict)
print(repr(a_frozen_dict)) # FDict: {'list': FList: (1, 2), 'set': FSet: {3, 4}}
Freezing a collection is achieved by following the following logic recursively:
- Immutable objects (except for collections) stay the same.
- Mapping (e.g. dict) frozen with FDict.
- Sequence (e.g. list or tuple) frozen as FLists.
- AbstractSet (e.g. set) frozen as FSets.
- If any value in the collection can't be frozen, an exception is raised.
- Type hints are only accurate as long as no data conversion was performed.
- support for thawing frozen collections.
- support for freezing more mutable types.