Immutable

class mosaic.types.immutable.ImmutableObject[source]

Bases: object

A class that inherits from ImmutableObject produces the same effect as a class with all attributes private in C++ programming, i.e. the attributes and methods of the instances of this class cannot be changed by code external to the class.

Examples

>>> class Klass(ImmutableObject):
>>>     def __init__(self):
>>>         self.attribute = 10
>>>
>>>     def change_attr(self):
>>>         self.attribute = 20
>>>
>>> klass = Klass()
>>> klass.attribute
10
>>> klass.attribute = 30
AttributeError('Objects of class "Klass" are immutable')
>>> klass.change_attr()
>>> klass.attribute
20