immutable

lsst.utils.classes.immutable(cls: _T) → _T

Decorate a class to simulate a simple form of immutability.

A class decorated as immutable may only set each of its attributes once; any attempts to set an already-set attribute will raise AttributeError.

Notes

Subclasses of classes marked with @immutable are also immutable.

Because this behavior interferes with the default implementation for the pickle modules, immutable provides 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 str and tuple, the immutable decorator 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.