为什么变量L在调用sorting(L)
函数时被改变?在其他语言中,L的副本会被传递给sorting()
函数,以便对x
的任何更改都不会改变原始变量。
def sorting(x):
A = x #Passed by reference?
A.sort()
def testScope():
L = [5,4,3,2,1]
sorting(L) #Passed by reference?
return L
>>> print testScope()
>>> [1, 2, 3, 4, 5]