shallow copy vs. deep copy

Uwe Mayer merkosh at hadiko.de
Sat Jun 22 12:28:49 EDT 2002


hi,

am am wondering where to draw the line between a shallow copy and a deep 
copy of compound objects.

when creating a shallow copy i could most easily create a new object and 
say 

new.__dict__ = old.__dict__

on the other side i could also:

new.__dict__ = old.__dict__.copy()

which creates a shallow copy of the old instance dictionary.
if all attributes of the class are primitive (like int, long, string) 
which one is the correct way of creating a shallow copy?

also: if i create a deep copy and my instance attributes only contain 
immutable objects there's no difference if i create another copy or just 
pass the reference. if one of the objects attribute gets "changes" it'll 
be reassigned a new object and thats it.
what's the "fine" way here?

Ciao
Uwe



More information about the Python-list mailing list