I want to execute a method with a copy of the original self
passed while execution.
Here is the code I'm talking about:
def protect_self(func):
from copy import copy
from functools import wraps
@wraps(func)
def decorated(self, *args, **kwargs):
self_copy = copy(self)
return func(self_copy, *args, **kwargs)
return decorated
In my understanding the copy function creates a new object of the same type and
copies the __dict__
of the old one to the new object (using references, so
changes to actual object instances in __dict__
will still affect the original object).
Does this mean I can be sure that the decorated method cannot modify __dict__
of
the original instance?
Just to make sure: I don't need a secure sandbox behaviour. My purpose is just
to have a single object instanciated which I will use as a factory. The
protected method should be possible to modify the passed self
but it should
be reseted afterwards.