开发者

Deepcopy a simple Python object

开发者 https://www.devze.com 2022-12-14 22:22 出处:网络
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 obj

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消