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.**
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
listofvaluesproperty 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 :
If you wanted to change the content of the list, and not the reference, you need to use slicing. That is :
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: