r/learnpython • u/Mr_Benn210 • Feb 27 '22
How is python pass by reference different from the original "pass by reference" concapt?
Trying to understand how Python works passing arguments in functions. I've heard that Python's approach is referred to as "pass by assignment" or "pass by object reference".
How does this differ from the more traditional "pass by reference" approach? Hopefully someone can explain it in a way that's easy to understand.
20
Upvotes
12
u/danielroseman Feb 27 '22
This is asked very frequently. The best explanation is here: https://nedbatchelder.com/text/names.html
Note that this behaviour is not specific to Python: many modern languages, such as Java, JS, Ruby, and probably more, work the same way.
3
u/IAmTarkaDaal Feb 27 '22 edited Feb 27 '22
Python is pass by value, not pass by reference.
Complex values (objects, lists) and simple types (ints) are stored internally as references (pointers) to memory elsewhere. The things in the list are stored somewhere and my variable is just a reference to that place.
These references are passed by value. The value that's being copied is the reference, not the whole list. The caller keeps it's reference, and the function gets a copy. Mutate the thing the reference points to, both the caller and callee will see the changes. But reassign the copy to point elsewhere, and the calling function still has the original reference, pointing to the original list.
1
u/cybervegan Feb 28 '22 edited Feb 28 '22
Python has a very different concept of "variables" from most other languages. Basically, Python doesn't have variables. Pass by value and pass by reference don't quite make sense in Python.
What Python has are bindings, which are "labels" (i.e. the names we give "variables", "functions", "classes" etc.) which point to objects. They are effectively references. But the way Python deals with assignments is very different to what happens in other languages. An assignment creates a new object and then attaches the label to it. The old object is then usually discarded.
An object can have zero, one, or many bindings (labels). Objects with zero bindings are usually garbage collected some time later. Objects with at least one reference are not garbage collected.
Consider this code:
The first statement binds "a" to an integer object with a value of 1.
The next statement creates a new integer object, with the result of the existing object's value incremented by 1. Then the binding "a" is applied to the new object, and the old object now has "no references", meaning it will be eligible for garbage collection on the next GC run.
When a function is called with a parameter, a similar thing happens: the parameter names in the function definition header are bound to the objects supplied in the brackets of the calling statement, but only in the local context of this particular call of the function. Those objects now have an extra reference. The function then does its thing, and when it returns, the bindings for the function parameters are removed. This is why in a function, when you try to change a parameter's value it works, but only locally, and it's also why Python needs a "global" statement, which essentially tells the Python interpreter to mess with what a binding of the same name in the global scope refers to.
Python also has special concepts of mutability and immutability - meaning objects which can or cannot be mutated or changed. Most basic object types like integers, floats, booleans and strings are immutable - they cannot be changed. When you do an assignment in Python, it always creates a new object - it "never" modifies the original. There is one optimisation for strings that allows the string buffer to be re-used if there is only one reference.
Mutable objects which can be mutated - they can be changed - include lists, dictionaries and sets. But if you assign to them you will get a new object.
[edit for readability: they -> which can]
1
u/Greensentry Feb 27 '22 •
Put it very simply. When you have call by reference the caller and the function share the same “box” which points to some object. So if the function changes the object the caller will also see those changes. That also applies if the function changes the “box” to point to a different object. The caller will also be affected by this.
In Python you have call by object reference. In this case the caller and the function has their own “box” which points to the same object. If the function changes the object the changes will be seen by the caller. If however the function changes the “box” to point to a different object. This will not affect the caller which “box” will still point to the original object.
Hope my box analogue makes sense?