Shallow vs Deep copies [was Re: python assignment]

Terry Reedy tjreedy at udel.edu
Wed Jul 23 18:11:54 EDT 2003


"Erik Max Francis" <max at alcyone.com> wrote in message
news:3F1EDFA0.ED78E04F at alcyone.com...

> immutable objects need not ever be literally copied

> Shallow copying makes a duplicate of the parent object.  Deep
copying
> makes a duplicate of everything.

To be more exact, because first statement is true,
second_statement.replace('everything', 'all mutables'),
which is to say, 'as much as needed'.

>>> import copy
>>> l1=[(),[]]
>>> l2=copy.deepcopy(l1)
>>> for i in l1+l2: print id(i)
...
7669904
8422784
7669904 #same ()
8428448 #different []

Terry J. Reedy







More information about the Python-list mailing list