A question about python deepcopy and shallow copy.
the post at What is the difference between a deep copy and a shallow copy?
cannot help me.
why e.g. 1 's sum is 6 not 10 ?
e.g.1 :
kvps = { '1' : 1, '2' : 2 }
theCopy = kvps.copy() # both point to the same mem location ?
kvps['1'] = 5
sum = kvps['1'] + theCopy['1']
print sum
output sum is 6
e.g.2 :
aList = [1,2]
bList = [3,4]
kvps = { '1' : aList, '2' : bList }
theCopy = kvps.copy() # both point to the same mem location ?
kvps['1'][0] = 5
sum = kvps['1'][0] + theCopy['1'][0]
print sum
output sum is 10
e.g.3 :
import copy
aList = [1,2]
bList = [3,4]
kvps = { '1' : aList, '2' : bList }
theCopy = copy.deepcopy(kvps)
kvps['1'][0] = 5
sum = kvps['1'][0] + theCopy['1'][0]
print sum
output sum is 6.
Also , e.g. 4
kvps = { '1' : 1, '2' : 2 }
theCopy = dict(kvps) # theCopy hold a reference to kvps ?
kvps['1'] = 5 # should also change theCopy , right ?
sum = kvps['1'] + theCopy['1']
print kvps
print theCopy
print sum
its sum is 6 , if theCopy is a reference to kvps , it should be 10.