Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > is dict.copy() a deep copy or a shallow copy

Reply
Thread Tools

is dict.copy() a deep copy or a shallow copy

 
 
Alex
Guest
Posts: n/a
 
      09-04-2005
Entering the following in the Python shell yields

>>> help(dict.copy)

Help on method_descriptor:

copy(...)
D.copy() -> a shallow copy of D

>>>


Ok, I thought a dictionary copy is a shallow copy. Not knowing exactly
what that meant I went to http://en.wikipedia.org/wiki/Deep_copy where
I could read

"...a deep copy is copy that contains the complete encapsulated data of
the original object, allowing it to be used independently of the
original object. In contrast, a shallow copy is a copy that may be
associated to data shared by the original and the copy"

Going back to Python shell I tested the following

>>> D={'Python': 'good', 'Basic': 'simple'}
>>> E=D.copy()
>>> E

{'Python': 'good', 'Basic': 'simple'}
>>> D['Basic']='oh my'
>>> D

{'Python': 'good', 'Basic': 'oh my'}
>>> E

{'Python': 'good', 'Basic': 'simple'}
>>>


Hmm, this looks like a deep copy to me?? I also tried

>>> D={'Python': 'good', 'Basic': 'simple'}
>>> E=D
>>> E

{'Python': 'good', 'Basic': 'simple'}
>>> E['Basic']='oh my'
>>> E

{'Python': 'good', 'Basic': 'oh my'}
>>> D

{'Python': 'good', 'Basic': 'oh my'}
>>>


which looks like a shallow copy to me?? So my hypothesis is that E=D is
a shallow copy while E=D.copy() is a deep copy.

So is the documentation wrong when they claim that D.copy() returns a
shallow copy of D, or did I misunderstand the difference between a deep
and shallow copy?

Thanks

 
Reply With Quote
 
 
 
 
Max Erickson
Guest
Posts: n/a
 
      09-04-2005
"Alex" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) oups.com:
>>>> D={'Python': 'good', 'Basic': 'simple'}
>>>> E=D.copy()
>>>> E

> {'Python': 'good', 'Basic': 'simple'}
>>>> D['Basic']='oh my'
>>>> D

> {'Python': 'good', 'Basic': 'oh my'}
>>>> E

> {'Python': 'good', 'Basic': 'simple'}
>>>>

>
> Hmm, this looks like a deep copy to me?? I also tried
>


It is shallow, but strings are immutable so the difference is fairly
moot.

>>>> D={'Python': 'good', 'Basic': 'simple'}
>>>> E=D
>>>> E

> {'Python': 'good', 'Basic': 'simple'}
>>>> E['Basic']='oh my'
>>>> E

> {'Python': 'good', 'Basic': 'oh my'}
>>>> D

> {'Python': 'good', 'Basic': 'oh my'}
>>>>

>


Here, E and D are different names for the same object. There is no
copy.

> which looks like a shallow copy to me?? So my hypothesis is that
> E=D is a shallow copy while E=D.copy() is a deep copy.
>


> So is the documentation wrong when they claim that D.copy()
> returns a shallow copy of D, or did I misunderstand the
> difference between a deep and shallow copy?


Sort of, some examples:

Here d1 and d2 are copies. They can be independently changed to
refer to different objects:

>>> d1={'a':1,'b':2}
>>> d2=d1.copy()
>>> d1['a']=3
>>> d2['b']=4
>>> d1

{'a': 3, 'b': 2}
>>> d2

{'a': 1, 'b': 4}

Again, d3 and d4 are copies, but instead of changing the objects
they refer to, we change the contents of the objects they refer to:

>>> d3={'c':[3],'d':[4]}
>>> d4=d3.copy()
>>> d3['c'][0]=5
>>> d4['d'][0]=6
>>> d3

{'c': [5], 'd': [6]}
>>> d4

{'c': [5], 'd': [6]}

Both cases are shallow copies. In a deep copy, altering the contents
of d3['c'] would have no impact on the contents of d4['c'].

max
 
Reply With Quote
 
 
 
 
Alex
Guest
Posts: n/a
 
      09-05-2005
Thanks max,
Now it's much clearer. I made the following experiment
>>>#1
>>> D={'a':1, 'b':2}
>>> D

{'a': 1, 'b': 2}
>>> E=D.copy()
>>> E

{'a': 1, 'b': 2}
>>> D['a']=3
>>> D

{'a': 3, 'b': 2}
>>> E

{'a': 1, 'b': 2}
>>>#2
>>> D={'a':[1,3], 'b':[2,4]}
>>> D

{'a': [1, 3], 'b': [2, 4]}
>>> E=D.copy()
>>> E

{'a': [1, 3], 'b': [2, 4]}
>>> D['a'][0]=9
>>> D

{'a': [9, 3], 'b': [2, 4]}
>>> E

{'a': [9, 3], 'b': [2, 4]}
>>>#3
>>> import copy
>>> F=copy.deepcopy(D)
>>> D

{'a': [9, 3], 'b': [2, 4]}
>>> F

{'a': [9, 3], 'b': [2, 4]}
>>> D['a'][0]=7
>>> D

{'a': [7, 3], 'b': [2, 4]}
>>> F

{'a': [9, 3], 'b': [2, 4]}
>>>


A shallow copy of an object is a copy of the first-level data members
and if one of the members is a pointer, then only the pointer value is
copied, not the structure pointed to by the pointer. The original
object and its shallow copy share the memory space occupied by these
structures.
A deep copy of an object is a copy of everything (including the
structures pointe to by the pointers). The original object and its deep
copy do not share any memory space.

In #1 no pointers are involved so changing a value affects only D but
not E.

In #2 D['a'] and E['a'] are pointers that both point to the same memory
space. Changing that memory space doesn't change the pointers
themsleves.

In #3 a deepcopy makes sure that a copy is made not just of the
pointers but also of the things pointed to.

So the lesson to be learned is that if you make a copy of a dictionary
whose values are pointers to other structures then a deepcopy prevents
a coupling between a the original and the copy.

Alex

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to implement a deep copy or shallow copy? shuisheng C++ 4 12-17-2006 08:40 AM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
How to overload operator= to have both deep copy and shallow copy semantics bluekite2000@gmail.com C++ 1 06-24-2005 05:28 PM
deep and shallow copy Tony Johansson C++ 5 05-19-2005 04:13 PM
deep/shallow copy - constructor v Object.copy() VisionSet Java 8 04-29-2004 10:41 PM



Advertisments