python中的函数调用后变量值被更改

我正在观察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中。

amysemic 回答:python中的函数调用后变量值被更改

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3067151.html

大家都在问