Python Variable Scope (passing by reference or copy?)
Why does the variable L gets manipulated in the sorting(L)
function call? In other languages, a copy of L would be passed through to sorting()
as a copy so that any changes to x
would not change the original variable?
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,开发者_Go百科 4, 5]
Long story short: Python uses pass-by-value, but the things that are passed by value are references. The actual objects have 0 to infinity references pointing at them, and for purposes of mutating that object, it doesn't matter who you are and how you got a reference to the object.
Going through your example step by step:
L = [...]
creates alist
object somewhere in memory, the local variableL
stores a reference to that object.sorting
(strictly speaking, the callable object pointed to be the global namesorting
) gets called with a copy of the reference stored byL
, and stores it in a local calledx
.- The method
sort
of the object pointed to by the reference contained inx
is invoked. It gets a reference to the object (in theself
parameter) as well. It somehow mutates that object (the object, not some reference to the object, which is merely more than a memory address). - Now, since references were copied, but not the object the references point to, all the other references we discussed still point to the same object. The one object that was modified "in-place".
testScope
then returns another reference to that list object.print
uses it to request a string representation (calls the__str__
method) and outputs it. Since it's still the same object, of course it's printing the sorted list.
So whenever you pass an object anywhere, you share it with whoever recives it. Functions can (but usually won't) mutate the objects (pointed to by the references) they are passed, from calling mutating methods to assigning members. Note though that assigning a member is different from assigning a plain ol' name - which merely means mutating your local scope, not any of the caller's objects. So you can't mutate the caller's locals (this is why it's not pass-by-reference).
Further reading: A discussion on effbot.org why it's not pass-by-reference and not what most people would call pass-by-value.
Python has the concept of Mutable and Immutable objects. An object like a string or integer is immutable - every change you make creates a new string or integer.
Lists are mutable and can be manipulated in place. See below.
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print a is b, a is c
# False True
print a, b, c
# [1, 2, 3] [1, 2, 3] [1, 2, 3]
a.reverse()
print a, b, c
# [3, 2, 1] [1, 2, 3] [3, 2, 1]
print a is b, a is c
# False True
Note how c was reversed, because c "is" a. There are many ways to copy a list to a new object in memory. An easy method is to slice: c = a[:]
It's specifically mentioned in the documentation the .sort()
function mutates the collection. If you want to iterate over a sorted collection use sorted(L)
instead. This provides a generator instead of just sorting the list.
a = 1
b = a
a = 2
print b
References are not the same as separate objects.
.sort()
also mutates the collection.
精彩评论