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.
delnan
source share