how to copy a Python object

Juho Schultz juho.schultz at helsinki.fi
Tue Feb 7 08:23:16 EST 2006


mitsura at skynet.be 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



More information about the Python-list mailing list