immutable¶
- lsst.utils.classes.immutable(cls: _T) _T¶
- Decorate a class to simulate a simple form of immutability. - A class decorated as - immutablemay only set each of its attributes once; any attempts to set an already-set attribute will raise- AttributeError.- 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 - strand- tuple, the- immutabledecorator provides a- __copy__implementation that just returns- self, 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 return- self. This is not done by the decorator, as it has no way of checking for recursive immutability.