frigidΒΆ
π A Python library package which provides immutable data structures - collections which cannot be modified after creation.
Key Features βΒΆ
π Immutable Dictionary: Like a regular dict, but entries cannot be modified or removed. Also has variant for validation on initialization. And provides set operations not found on MappingProxyType.
ποΈ Immutable Namespace: Similar to SimpleNamespace, but attributes are immutable from creation.
𧱠Additional Types: Classes (including abstract base classes), modules, and objects with immutable behavior.
ποΈ Flexible Initialization: Support for unprotected attributes during initialization; useful for compatibility with class decorators, such as dataclasses.
π Flexible Mutability: Support for declaring specific attributes as mutable, enabling selective modification while maintaining immutability for other attributes.
Installation π¦ΒΆ
pip install frigid
Note on Immutability π’ΒΆ
Enforcement of immutability is quite difficult in Python. While this library enforces immutability by default, it can be circumvented by anyone who has intermediate knowledge of Python machinery and who is determined to circumvent the immutability. Use the library in the spirit of making programs safer, but understand that it cannot truly prevent unwanted state tampering.
Examples π‘ΒΆ
Immutable Namespaces ποΈΒΆ
An immutable namespace, similar to types.SimpleNamespace
, is available.
This namespace can be initialized from multiple iterables and from keyword
arguments. (Keyword arguments shown below; see documentation for additional
forms of initialization.)
>>> from frigid import Namespace
>>> ns = Namespace( apples = 12, bananas = 6 )
>>> ns.cherries = 42 # β Attempted assignment raises error.
Traceback (most recent call last):
...
frigid.exceptions.AttributeImmutabilityError: Cannot assign attribute 'cherries'.
>>> del ns.apples # β Attempted deletion raises error.
Traceback (most recent call last):
...
frigid.exceptions.AttributeImmutabilityError: Cannot delete attribute 'apples'.
>>> ns
frigid.namespaces.Namespace( apples = 12, bananas = 6 )
Immutable Dictionaries πΒΆ
An immutable dictionary, similar to dict
, is available. This dictionary can
be initialized from multiple iterables and from keyword arguments. (Keyword
arguments shown below; see documentation for additional forms of
initialization.)
>>> from frigid import Dictionary
>>> dct = Dictionary( apples = 12, bananas = 6)
>>> dct['cherries'] = 42 # β Attempted assignment raises error.
Traceback (most recent call last):
...
frigid.exceptions.EntryImmutabilityError: Cannot assign entry for 'cherries'.
>>> del dct['bananas'] # β Attempted removal raises error.
Traceback (most recent call last):
...
frigid.exceptions.EntryImmutabilityError: Cannot delete entry for 'bananas'.
>>> dct
frigid.dictionaries.Dictionary( {'apples': 12, 'bananas': 6} )
Immutable Objects π§±ΒΆ
The immutable
decorator can be applied to any class to make its instances fully immutable after initialization.
>>> from frigid import immutable
>>> @immutable
... class Config:
... def __init__( self, debug = False ):
... self.debug = debug
...
>>> config = Config( debug = True )
>>> config.verbose = True # β Attempted addition raises error
Traceback (most recent call last):
...
frigid.exceptions.AttributeImmutabilityError: Cannot assign or delete attribute 'verbose'.
>>> config.debug = False # β Attempted reassignment raises error
Traceback (most recent call last):
...
frigid.exceptions.AttributeImmutabilityError: Cannot assign or delete attribute 'debug'.
Use Cases π―ΒΆ
π Configuration Objects: Objects which must maintain consistent state throughout program execution.
π Value Objects: Objects which represent values and should be immutable, like numbers or strings.
𧱠Immutable Collections: Many scenarios requiring collections with complete immutability guarantees.