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!