Deepcopy a simple Python object
I have an object which defines a __deepcopy__
method. I would like a function that will开发者_如何学JAVA 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.
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?
精彩评论