how to copy a Python object
Hi, I am new to Python and OO programming. I need to copy a Python object (of a class I made myself). new_obj = old_object doesn't seem to work since apparently new_obj is then a referrence to old_obj. I found out that there is a module called 'copy' that allows you to do a shallow or a deep copy. I need a deep copy since my object contains dicts to other objects that also need to be copied. However, when I do new_object = copy.deepcopy(old_object) I get a series of errors. The most important one: TypeError: object.__new__(PySwigObject) is not safe, use PySwigObject.__new__() So any help much appreciated. With kind regards, Kris I am using Python 2.4 -- http://mail.python.org/mailman/listinfo/python-list
Re: how to copy a Python object
[EMAIL PROTECTED] a écrit : Hi, I am new to Python and OO programming. I need to copy a Python object (of a class I made myself). new_obj = old_object doesn't seem to work since apparently new_obj is then a referrence to old_obj. it is I found out that there is a module called 'copy' that allows you to do a shallow or a deep copy. I need a deep copy since my object contains dicts to other objects that also need to be copied. yes but ... However, when I do new_object = copy.deepcopy(old_object) I get a series of errors. The most important one: TypeError: object.__new__(PySwigObject) is not safe, use PySwigObject.__new__() you see ? that illustrates there is no general solution to the deep copy problem So any help much appreciated. just provide your own copy() operator on your class you and only you can know how deep you want to copy things (it just depends on the nature of said things) With kind regards, Kris I am using Python 2.4 -- http://mail.python.org/mailman/listinfo/python-list
Re: how to copy a Python object
Already thanks for the reply, but how to write your own copy operator? Won't you always be passing referrences to new_obj? -- http://mail.python.org/mailman/listinfo/python-list
Re: how to copy a Python object
[EMAIL PROTECTED] wrote: Already thanks for the reply, but how to write your own copy operator? Won't you always be passing referrences to new_obj? for example this would work class X(object): ... def __init__(self,lst): ... self.lst = lst ... def copy(self): ... return X(self.lst[:]) ... def __str__(self): ... return lst has id %i % id(self.lst) ... x=X([1,2,3]) y=x.copy() print x lst has id 1078097132 print y lst has id 1078097228 but I don't like that in this case self.lst must be passed through __init__ I can think of another variant class Y(object): ... def fill(self): ... self.lst = [randint(i*10,(i+1)*10) for i in xrange(5)] ... def __repr__(self): ... return lst has id = %i % id(self.lst) ... def copy(self): ... ret = Y() ... ret.lst = self.lst[:] ... return ret ... from random import randint y=Y() y.fill() y lst has id = 1078097452 print y lst has id = 1078097452 x=y.copy() x lst has id = 1078097004 ... anyone? are there better approaches? Regards, Daniel -- http://mail.python.org/mailman/listinfo/python-list
Re: how to copy a Python object
[EMAIL PROTECTED] wrote: Already thanks for the reply, but how to write your own copy operator? Won't you always be passing referrences to new_obj? If you need to modify the behaviour of copy or deepcopy, you can give your class __copy__ and __deepcopy__ methods. Then copy.copy and copy.deepcopy will use these. As a silly example, you could use: def __copy__(self): raise IOError, 'Permission denied.' http://www.python.org/doc/2.4.2/lib/module-copy.html -- http://mail.python.org/mailman/listinfo/python-list
Re: how to copy a Python object
Schüle Daniel [EMAIL PROTECTED] wrote: ... class X(object): ... def __init__(self,lst): ... self.lst = lst ... def copy(self): ... return X(self.lst[:]) ... def __str__(self): ... return lst has id %i % id(self.lst) ... ... anyone? are there better approaches? def __getstate__(self): return self.lst def __setstate__(self, L): self.lst = L and copy an instance x of X with copy.copy(x) [[after import copy, of course]]. This will also support pickling c. getstate/setstate are generally the best approach. An equally good alternative here, omit setstate and just: def __getstate__(self): return dict(lst=self.lst) Alex -- http://mail.python.org/mailman/listinfo/python-list