我正在观察python函数之间变量值中的异常行为,我无法理解其背后的正确原因。我有以下两个功能
def a():
seg_map = np.zeros([224,224],dtype=float)
while True:
print("Calling expand foreground method")
new_seg_map = expandforeground2(seg_map,cams_dict[name],z,seg_result,hypercols)
print(seg_map)
print(np.array_equal(seg_map,new_seg_map))
if np.array_equal(new_seg_map,seg_map):
print('break')
break
seg_map = new_seg_map
def expandforeground2(seg_map,cam_map,layer_no,hypercol_result):
threshold = np.amax(hypercol_result) * 0.8
temp_map = seg_map
#do some manipulations on temp_map
return temp_map
现在,从函数expandforeground2
调用a
函数后,我看到函数seg_map
中的a
值已被修改为expandforeground2
函数中的值并且np.array_equal(seg_map,new_seg_map)
始终为True
。我不明白为什么会这样。我已经看到一些相关的问题,但没有一个解决我的问题。有人可以向我解释这背后的原因以及如何解决此问题吗?
我希望函数seg_map
中的a
的值保持不变,并将expandforeground2
函数的返回值存储在new_seg_map
中。