我使用下面的程序返回修改:變量修改的功能,當它不應該
def coordsRelToPoint(origin,point):
result = (int(origin[0]) - int(point[0]) , int(origin[1]) - int(point[1]))
return result
def initCoordsRelToStartBattle(calibration):
print ('FUNCTION BEGIN')
print ('orig: %s' % str(calibration['center']))
print ('')
new_calibration = {}
new_calibration = calibration
print ('orig: %s' % str(calibration['center']))
print ('')
new_calibration['center'] = coordsRelToPoint((774,454),calibration['center'])
print ('orig: %s' % str(calibration['center']))
print ('')
print ('new: %s' % str(new_calibration['center']))
print ('')
print ('FUNCTION END')
return new_calibration
def main():
calibration = {}
calibration['center'] = (156,20)
initCoordsRelToStartBattle(calibration)
if __name__ == "__main__":
main()
它打印屏幕上的以下內容:
FUNCTION BEGIN
orig: (156, 20)
orig: (156, 20)
orig: (618, 434)
new: (618, 434)
FUNCTION END
我不明白這就是爲什麼我的原始變量在調用函數時被修改,因爲它在另一個函數中被使用。
我不應該看到行的原點:(618,434),因爲這意味着我的變量校準已被修改,即使它沒有從函數返回。
這不是一個副本,甚至不是淺的;它只是對* same *對象的另一個引用。 –
@DanielRoseman你是對的,我解決了錯誤的解釋。謝謝! – albertoql