1
import numpy as np  

var_np = np.zeros(shape=[2])

dic_orig = {'var_np':var_np.copy(), 'var_int':0}

dic_copy = dic_orig.copy()

dic_copy['var_np'] += 1
dic_copy['var_int'] += 1

#problem
print(dic_orig['var_int']) #ouput: 0  -->Didn't chagne, Good! 
print(dic_orig['var_np']) #ouput:[1,1] -->This change, Bad!

#The output shows that this two have different id
#The how would the problem happen?
print(id(dic_orig['var_int'])) #ouput:1474134096
print(id(dic_copy['var_int'])) #ouput:1474134128

As shown in the code demo,

when the element of a dictionary are a numpy array,

the element of the original dictionary would change if I chagne the copy version of this dictionary.

This might causes some potential and latent bugs,

and actually I don't want this happen.

Why would this happen? Didn't the copy operation isolates the original and copy-verision dictionary? How can I solve this problem?

I have google it, and seems like no one ask it before. Thank you so much for your sincerely help!

WBR
  • 57
  • 1
  • 11

2 Answers2

1

The copy() method of a dict is a shallow copy:

dct1 = {'a': [1, 2, 3]}
dct2 = dct1.copy()
dct1['a'] is dct2['a']
# True

If you want a deep copy, you can use deepcopy from the copy module:

from copy import deepcopy
dct2 = deepcopy(dct1)
dct1['a'] is dct2['a']
# False
jakevdp
  • 77,104
  • 11
  • 125
  • 160
0

This is the difference between a "shallow" copy and a "deep copy".

In a shallow copy, the object fields are copied over as references. If you change one, the other also changes since the reference is to the same memory address. In a deep copy, actual new copies of objects get created in entirely new addresses.

Instead of using .copy(), you should use copy.deepcopy().

Jabrove
  • 718
  • 5
  • 13