0

I saw similar question here on stackoverflow but it wasn't satisfactorily answered. (link to this question - How to copy a python class? )

So my question is, is there any possible way to create a copy of class (instance of a class), that is stored in variable A and assign this copy of class to new variable B without having these variables A and B mutually dependent?

To be clear - is there a quite simple way?

Model situation would be:

class B:
    def __init__(self, li):
        self.l = li * 5
        self.u = 5       

class A:
    def __init__(self):
        self.x = [1, 2, 3]
        self.c = B([45, 1, 3])
    self.i = 1

x = A()
y = x #This copies only reference to x
4
  • Care to link to that question? Commented Jan 18, 2013 at 15:31
  • 1
    By "copy of class" I assume you mean "instance of a class"? Commented Jan 18, 2013 at 15:31
  • @Jendas note that the question you point to is not about copying instances, but the class itself! Commented Jan 18, 2013 at 15:35
  • The question is fundamentally different then, as it is talking about the class itself. Edit: Beaten to the punch. Commented Jan 18, 2013 at 15:35

3 Answers 3

4

Implement __copy__() for your class, then you can use copy.copy() to create a copy of the instance. This can't be done automatically due to the nature of Python objects (there is no way of knowing what variables will be set on an instance, and no way of knowing how it's been mutated, it's up to you to ensure that data is copied correctly).

Normally, presuming a class that functions in a normal way, __copy__() will probably be something simple like this for a supposed class SomeClass:

def __copy__(self):
    return SomeClass(self.somefield, self.otherfield, self.somethingelse)

Naturally, exact implementation will vary wildly based on the class itself.

Sign up to request clarification or add additional context in comments.

7 Comments

I thought so. But I'm not sure if I really understand why this can not be done automatically. Can't I walk through the class recursively and every time I hit a mutable copy its elements to whole new mutable and continue like this?
@Jendas Well, you could perform copies of everything in the object's __dict__ or something hacky like that, but it's possible there is data in there that it doesn't make sense to copy over. Given we can monkey patch anything into a Python class, copying like this could produce unexpected results. When we copy we expect something with a new identity, but the same values - in some cases, the data in the object could be about identity, and copying it across would be wrong - Python can't automate stuff like that.
Another example, say the constructor does something outside of the class - this won't happen when we copy and object in this way, as the constructor won't get run (if it did, we would then introduce a requirement for an empty constructor to work, and do the right thing, and it quickly becomes more complex than doing it manually).
Another issue is that some of the attributes of the object could be properties, and if you access them through the instance they will lose their property-ness, which would be bad. The easiest way to deal with this is to have the instance know how to copy itself.
@kindall Exactly - Python being the language it is, there are probably tons of little edge cases like this that make it, at the very least, impractical to automate the task.
|
1

If you want to create a separate copy of an object, try the copy module.

Comments

0

Using deepcopy in copy module is simpler. Erm, guys here's solution to those subtle problems: http://docs.python.org/2/library/copy.html#copy.deepcopy

1 Comment

-1, Read further down that page, just like copy() needs __copy__(), deepcopy() requires you to implement __deepcopy__() on a class.

Your Answer

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.