Take the following code
#module functions.py
def foo(input, new_val):
input = new_val
#module main.py
input = 5
functions.foo(input, 10)
print input
I thought input would now be 10. Why is this not the case?
Python always uses pass-by-reference values. There isn't any exception. Any variable assignment means copying the reference value.
All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function.
Python utilizes a system, which is known as “Call by Object Reference” or “Call by assignment”. In the event that you pass arguments like whole numbers, strings or tuples to a function, the passing is like call-by-value because you can not change the value of the immutable objects being passed to the function.
In Python, arguments are always passed by value, and return values are always returned by value. However, the value being returned (or passed) is a reference to a potentially shared, potentially mutable object.
Everything is passed by value, but that value is a reference to the original object. If you modify the object, the changes are visible for the caller, but you can't reassign names. Moreover, many objects are immutable (ints, floats, strings, tuples).
Inside foo, you're binding the local name input
to a different object (10
). In the calling context, the name input
still refers to the 5
object.
Assignment in Python does not modify an object in-place. It rebinds a name so that after input = new_val
, the local variable input
gets a new value.
If you want to modify the "outside" input
, you'll have to wrap it inside a mutable object such as a one-element list:
def foo(input, new_val):
input[0] = new_val
foo([input])
Python does not do pass-by-reference exactly the way C++ reference passing works. In this case at least, it's more as if every argument is a pointer in C/C++:
// effectively a no-op!
void foo(object *input, object *new_val)
{
input = new_val;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With