Get a python_frozen reference to an object.
Frozen creates an immutable reference to an object. This works (lazily) recursive: mutable members are also frozen upon access. It keeps the original object mutable, returning a proxy class that behaves just like the object except being uneditable. If the underlying object changes, the proxy will reflect this automatically. As such it is immutable in the sense that the reference can’t be used to change it, not in the sense that it cannot be changed at all (you could of course discard the original mutable reference). Frozen objects can not be used as dictionary keys.
pip install frozenobj
Usage is very simple:
from frozenobj import frozen immutable_obj = frozen(mutable_obj)
As a simple example:
class Cls: def __init__(self): self.li = [37, 42] original = Cls() freezered = frozen(original) original.li.append(99) # works freezered.li.append(99) # ImmutableError
Have a look at tests.py for more examples.
frozenmethod is idempotent and uses caching: if you call it twice on the same object, either successively or in parallel, you get the same proxy.
Frozen is available under the revised BSD license, see LICENSE.txt. You can do anything as long as you include the license, don’t use my name for promotion and are aware that there is no warranty.