Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shallow copy, why does the list not change

Tags:

python

copy

I am trying to understand the difference between shallow copy and deep copy in python. I read many posts here and they have been helpful. However, I still don't understand the difference well. Can someone please explain the reason for the result below. The result that I do not understand is indicated in the comments.

Thanks very much.

import copy
import random

class understand_copy(object):
    def __init__(self):
        self.listofvalues = [4, 5]

    def set_listofvalues(self, pos, value):
        self.listofvalues[pos] = value

ins = understand_copy()

newins = copy.copy(ins)

newins.set_listofvalues(1,3)
print "ins = ", ins.listofvalues
print "in newins", newins.listofvalues
# Gives the following output as I would expect based on the explanations.
# prints ins = [4, 3]
# prints newins = [4, 3]


newins.listofvalues.append(5)
print "ins =", ins.listofvalues
print "newins =", newins.listofvalues
# Gives the following output as I would expect based on the explanations.
# prints ins = [4, 3, 5]
# prints newins = [4, 3, 5]


newins.listofvalues = [10, 11]
print "ins = ", ins.listofvalues
print "newins = ", newins.listofvalues
# Gives
# ins = [4, 3, 5]
# newins = [10, 11]
# This is the output that I do not understand. 
# Why does ins.listofvalues not change this case.**
like image 808
Curious2learn Avatar asked Dec 29 '22 03:12

Curious2learn


1 Answers

In Python, object's field keep reference to object. So when you assign the new list in your exemple, you're changing the object that is referenced by the field, not its content. Before the affectation, the listofvalues property of both object where referencing the same list, but after the affectation, they are referencing two different list.

This is equivalent to the following code :

>>> a = [4, 5]
>>> b = a
>>> b.append(3)
>>> b
[4, 5, 3]
>>> a
[4, 5, 3]
>>> b = [6, 7]
>>> b
[6, 7]
>>> a
[4, 5, 3]

If you wanted to change the content of the list, and not the reference, you need to use slicing. That is :

>>> a = [4, 5, 3]
>>> b = a
>>> b[:] = [6, 7]
>>> b
[6, 7]
>>> a
[6, 7]

NOTE: the following is based on my understanding of the internals of Python 2.6. It is thus really implementation specific, however the mental model it gives you is probably pretty close of how the language rule are written and should work with any implementation.

In Python, objects are always accessed through references (as in Java, not C++). However, a variable name or attribute name can be though as a binding in a dictionary, and are implemented as such in CPython (except for the local variable optimization, the presence of __slots__, or the pseudo-attributes exposed via __getattr__ and friends).

In Python, each object as a private dictionary mapping each of its attribute name to the value. And the interpreter has two private dictionary holding the mapping between local and global variable name to their value. When you change the value of a variable or attribute of an object, you are just changing the binding in the corresponding dictionary.

So in you example, you have the same behavior as in the following code:

def understand_copy():
   return {'listofvalues': [4, 5]}

def deepcopy(obj):
   if instance(obj, dict):
       copy = {}
       for key, value in obj.iteritems():
           copy[key] = deepcopy(value)  # Note the recursion
       return copy
   if instance(obj, list):
       copy = []
       for value in obj:
           copy.append(deepcopy(value)) # Note the recursion
       return copy
   return obj

def copy(obj):
   if instance(obj, dict):
       copy = {}
       for key, value in obj.iteritems():
           copy[key] = value  # No recursion this time, the copy is shallow
       return copy
   if instance(obj, list):
       copy = []
       for value in obj:
           copy.append(value) # No recursion this time, the copy is shallow
       return copy
   return obj

globals = {}
globals['ins'] = understand_copy()
globals['new'] = copy(global['ins'])
# Now globals['ins']['listofvalues']
# and globals['new']['listofvalues']
# reference the same object!

globals['ins']['listofvalues'].__setitem__(0, 3)
globals['ins']['listofvalues'].append(5)
# We are calling function on one object,
# but not changing a binding, so the two
# 'listofvalues' attribute still refers
# to the same object.

globals['ins']['listofvalues'] = [10, 11]
# Now we changed the binding of the name
# in the dictionary 'ins' so now the two
# objects attributes points to different
# lists.
like image 62
Sylvain Defresne Avatar answered Dec 30 '22 16:12

Sylvain Defresne