1

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.

asked Dec 19, 2009 at 17:44

1 Answer 1

4

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?

answered Dec 19, 2009 at 17:50
1
  • 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! Commented Dec 19, 2009 at 17:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.