[Tutor] Re: copies deep and shallow

Andrei project5 at redrival.net
Fri Feb 13 16:10:22 EST 2004


Marilyn Davis wrote on Fri, 13 Feb 2004 09:21:31 -0800 (PST):

<snip>
> So shallow copies are independent at the first level but dependent at
> deeper levels.

I'm not sure what you mean by "deeper levels". If your whatever you copy
only contains immutable components (string, numbers, tuples), there isn't
really any danger in this because you can't change them in-place anyway
(ragardless of how many levels deep the tuple is for example).
 
> Why would someone want a shallow copy?  It seems like trouble lurking.

Try this in the interactive interpreter: 

a = range(2000000) # make that 4mln if you have 512 MB or 1mln for 128MB
b = copy.deepcopy(a)
c = copy.copy(a)

Noticed any difference? Also it's not dangerous if you're using immutable
items.

> I would think you'd either want a completely deep copy, or a reference to
> the same everything.

Generally I use deepcopy just to be on the safe side, but that's not such a
big problem since I don't tend to use very large amounts of data when
copying.

-- 
Yours,

Andrei

=====
Mail address in header catches spam. Real contact info (decode with rot13):
cebwrpg5 at jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.




More information about the Tutor mailing list