A deep copy never copies the class definition, only the instance attributes.
It instead creates a new instance. Python instances only hold a reference to the class definition. Just call MyMethod
on that instance:
VMPast.MyMethod()
provided the class has such a method.
It is your specific class that prevents a successful deep-copy:
class VariableManagerClass:
def __getattr__(self, attr):
return AttributeSetter(self, attr)
The copy.deepcopy()
function looks for a .__deepcopy__()
method on the instance, and your __getattr__
hook instead returns a AttributeSetter
class.
You can prevent this by testing your attr
a little first:
class VariableManagerClass:
def __getattr__(self, attr):
if attr.startswith('_'):
raise AttributeError(attr)
return AttributeSetter(self, attr)
This signals that your class does not handle private attributes or special method names.