how to copy a Python object

M

mitsura

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
 
A

aurelien.campeas

(e-mail address removed) 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)
 
M

mitsura

Already thanks for the reply,

but how to write your own copy operator? Won't you always be passing
referrences to new_obj?
 
J

Juho Schultz

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
 
?

=?ISO-8859-1?Q?Sch=FCle_Daniel?=

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
.... 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
.... 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
....lst has id = 1078097004

.... anyone?
are there better approaches?

Regards, Daniel
 
A

Alex Martelli

Schüle Daniel said:
... 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top