shallow copy vs. deep copy 
Author Message
 shallow copy vs. deep copy

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



Thu, 09 Dec 2004 00:28:49 GMT  
 shallow copy vs. deep copy


Quote:
> 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__

hat way both object have the same namespace, don't they?. thats more like
the Borg pattern (Singleton replacement). if you change an attribute in one
object it will apear in the other object too.

Quote:
> on the other side i could also:

> new.__dict__ = old.__dict__.copy()

that's what i would call a swallow copy.

Quote:
> which creates a shallow copy of the old instance dictionary.
> if all attributes of the class are primitive (like int, long, string)
...
> 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?

not copying immutable objects is good to save memory, but it might not be
so easy to know if an object is immutable. ok you can check types for
tuples, strings, numbers etc. but you can't do much (at least simple) about
instances of user objects. it might be overkill for a first shoot to
optimize this.

viel spass,
chris

--



Thu, 09 Dec 2004 00:42:43 GMT  
 shallow copy vs. deep copy

Quote:

> > on the other side i could also:

> > new.__dict__ = old.__dict__.copy()

> that's what i would call a swallow copy.

A european or an african swallow?

SCNR ;-)

   Bernhard

--
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                           http://www.mapit.de/



Thu, 09 Dec 2004 01:05:21 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Method literals and deep/shallow copy

2. novice question: shallow and deep copies

3. deep and shallow copy, what does the 'copy' method do ?

4. 64bit MMX Memory copy vs 32bit nonMMX copy

5. Deep vs. Shallow binding

6. CLOS shallow copy

7. thinkings on shallow copy

8. shallow copy's

9. Custom Deep Copy

10. Deep Copy Needed

11. ABC newbie: How do I use deep assignment to copy records

12. Deep copy in Rub?

 

 
Powered by phpBB® Forum Software