我有一个函数,在其中使用局部变量,然后在函数完成后传回最终变量。我想要记录下函数执行前该变量的值,但全局变量也会随着局部变量的更新而更新。以下是我代码的简化版本(它相当长)。
我的问题是,当Llocal被更新时,L和Linitial都被更新了,但我希望Linitial不改变。P没有改变,所以我对这里发生的事情感到困惑。能帮忙吗?谢谢!
勇敢的人可以在这里找到整个代码:https://docs.google.com/document/d/1e6VJnZgVqlYGgYb6X0cCIF-7-npShM7RXL9nXd_pT-o/edit
def Turn(P,Llocal,T,oflag):
#The function here changes P, Llocal and T then passes those values back
return(P, Llocal, T, oflag)
#Later I call the function
#P and L are defined here, then I copy them to other variables to save
#the initial values
P=Pinitial
L=Linitial
P,L,T,oflag = Turn(P,L,T,oflag)
我的问题是,当Llocal被更新时,L和Linitial都被更新了,但我希望Linitial不改变。P没有改变,所以我对这里发生的事情感到困惑。能帮忙吗?谢谢!
勇敢的人可以在这里找到整个代码:https://docs.google.com/document/d/1e6VJnZgVqlYGgYb6X0cCIF-7-npShM7RXL9nXd_pT-o/edit
CapWords
保留给类名。 - Gareth Latty