I have an object which defines a __deepcopy__
method. I would like a function that will deepcopy it not by the method given by it, but in the default way that objects of the class object
are copied.
How could I do that? I think I could try to code it but there are probably many "gotchas" I won't be thinking of.
The reason I'm doing it is because I have an object class which implements a __deepcopy__
method, and that method checks for some condition, and in some cases it will deepcopy the object in a certain way, and in other cases it will deepcopy in the default object
way.
1 Answer 1
You basically need to override the existing __deepcopy__
method, which means temporarily setting the object's class to something different -- whether that's acceptable essentially depends on whether the "__deepcopy__
override" needs to affect only one, "top-level" object (in which case the kludge's probably OK), or if there are many objects of that class in the graph you're copying, in which case it's quite a mess. Which case obtains?
-
I think there would be multiple objects of that class in the tree, sorry. Also, I am scared by the thought of changing the object's class!Ram Rachum– Ram Rachum2009年12月19日 17:55:46 +00:00Commented Dec 19, 2009 at 17:55