Python Variable Scope (pass by reference or copy?)

Why is the variable L processed when the sorting(L) function is called? In other languages, a copy of L will be passed to sorting() as a copy so that any changes to x do 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, 4, 5] 
+7
source share
4 answers

In short: Python uses pass-by-value, but things that are passed by value are links. Actual objects have links from 0 to infinity pointing to them, and for the purpose of mutating this object, it does not matter who you are or how you got the link to the object.

Step by step:

  • L = [...] creates a list object somewhere in memory, a local variable L stores a reference to this object.
  • sorting (strictly speaking, a called object called the global name sorting ) is called with a copy of the link stored in L and stores it in the local name x .
  • The sort method of the object is called, which is referenced by the reference contained in x . It receives a reference to the object (in the self parameter). It somehow mutates this object (an object, not some link to an object that is just bigger than the memory address).
  • Now, since the links were copied, but not the object referenced, all the other links that we discussed still point to the same object. One object that has been modified "in place".
  • testScope then returns another reference to this list object.
  • print uses it to request a string representation (calls the __str__ method) and displays it. Since it is still the same object, of course it prints a sorted list.

Therefore, whenever you transfer an object anywhere, you share it with whoever receives it. Functions can (but usually will not) mutate the objects (referenced) that they pass from invoking mutation methods to assign members. Note that the purpose of this element is different from the simple ol name, which simply means changing your local area, not the calling object. Thus, you cannot mutate the callers' locators (therefore, it is not transmitted by reference).

Further reading: A discussion on effbot.org about why it doesn’t follow the link, and not what most people would call the -value pass.

+11
source

Python has the concept of Mutable and Immutable Objects . An object, such as a string or an integer, is immutable - each change you make creates a new string or integer.

Lists are changeable and can be managed locally. 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 

Notice how c was canceled 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[:]

+8
source

It is specifically mentioned in the documentation, the .sort() function mutates the collection. If you want to sorted(L) over a sorted collection, use sorted(L) . This provides a generator instead of sorting the list.

+1
source
 a = 1 b = a a = 2 print b 

Links do not match individual objects.

.sort() also mutates the collection.

0
source

All Articles