Is there a way to make a compound Python object immutable ‘in-depth’? That is, to disallow any change at any level to the object.
One can indeed make a deepcopy
of the object to work with, but that can be costly. I'd like to see if one can make the compound object completely immutable, perhaps by recursively overriding the internal __setattr__
methods to emulate immutability.
An example would be a dictionary which has dictionaries and Numpy arrays as its values, or a Numpy array which stores Python objects as its elements.
This is related to the SO question, “How to make an immutable object in Python?”.