immutable¶
-
lsst.daf.butler.core.utils.immutable(cls: _T) → _T¶ A class decorator that simulates a simple form of immutability for the decorated class.
A class decorated as
immutablemay only set each of its attributes once; any attempts to set an already-set attribute will raiseAttributeError.Notes
Subclasses of classes marked with
@immutableare also immutable.Because this behavior interferes with the default implementation for the
picklemodules,immutableprovides implementations of__getstate__and__setstate__that override this behavior. Immutable classes can then implement pickle via__reduce__or__getnewargs__.Following the example of Python’s built-in immutable types, such as
strandtuple, theimmutabledecorator provides a__copy__implementation that just returnsself, because there is no reason to actually copy an object if none of its shared owners can modify it.Similarly, objects that are recursively (i.e. are themselves immutable and have only recursively immutable attributes) should also reimplement
__deepcopy__to returnself. This is not done by the decorator, as it has no way of checking for recursive immutability.