Consider the following
from copy import deepcopy
c = {'username': 'admin', 'machines': ['foo', 'bar', 'baz']}
dc = c.copy()
d = deepcopy(dc)
d['username'] = 'mln'
d['machines'].remove('bar')
print d
print c
the result is as follows:
{'username': 'mln', 'machines': ['foo', 'baz']}
{'username': 'admin', 'machines': ['foo', 'bar', 'baz']}
but when using shallow copy, things will be different.
a = {'username': 'admin', 'machines': ['foo', 'bar', 'baz']}
b = a.copy()
b['username']='mln'
b['machines'].remove('bar')
print b
print a
the result is as follows:
{'username': 'mln', 'machines': ['foo', 'baz']}
{'username': 'admin', 'machines': ['foo', 'baz']}
So for the key
"machines"in dictd, when using deep copy, Python will create a new list object which is different to the list object in dictc.But when using shallow copy, there's only one list object and both dictionaries point at it.
What's more, when I use shallow copy, for objects like tuple and str, it creates a new object.
But for dictionary and list,it just adds another reference. When using deep copy, both create a new object.
Am I right?