Pass by Value in Python | Pass by Reference
When we define a custom or user defined function in Python we may, optionally we may need to specify parameter names between the function’s parentheses.
If we specify parameters in the function definition, then we need to pass argument values to the function’s parameters while calling it.
The function use that passed values during its execution by referencing it via parameter name.
Basically, there are two ways to pass argument values to the function’s parameters. They are:
- Call/Pass by Value
- Call/Pass by Reference
Call/Pass by Value in Python
In pass by value (also known as call by value), the argument passed to the function is the copy of of its original object. If we change or update the value of object inside the function, then original object will not change.
If the argument is variable, the copy of the current value of the variable is passed to the function’s parameter. The value of the variable in the function call is not affected by what happens inside the function.
Some of the programming languages like C++, Java uses call by value or pass by value concept to pass argument values to the function’s parameters.
If the argument is passed by value, a copy of it is made and passed to the function. If the argument values being passed to the function is large, the copying can take up a lot of time and memory.
That’s why Python language does not support the pass by value concept.
Call/Pass by Reference
In pass by reference (also known as call by reference), the argument passed to the function’s parameter is the original object. If we change the value of object inside the function, the original object will also change.
The scripting language like JavaScript uses the pass by reference mechanism to pass an object as an argument to the function. Python language does not support the pass by reference mechanism to pass argument to the function.
Pass by Object Reference in Python
In many books, the authors write Python supports both mechanisms: pass by value and pass by reference. It uses the pass by value mechanism to pass primitive type argument values.
Immutable arguments, such as integers, floats, strings, and tuples, are passed by value. A copy of them is made and passed to the function. If they are modified inside the function, the original will not update.
On the other hand, mutable arguments, such as lists, dictionaries, sets, and instances of classes, are passed by reference mechanism. They can be modified inside the function.
Some standard book writers say Python passes argument values to the function using the pass by reference technique. It does not support pass by value.
So here the question is:
Is Python pass by value or pass by reference?
Neither of these mechanisms of argument passing applies to Python. In Python programming language, the mechanism is pass by object reference. This technique is unique as compared to other programming languages.
We know that almost everything in Python is an object. Thus, every value is an object in Python, and all values contain references to actual objects.
In other words, we can say, object references that refer to the actual objects are passed by value. We call this mechanism as pass by object reference in Python.
Key Points about Pass by Object Reference
There are the following important points about the pass by object reference you should keep it mind.
- In the Python programming language, all argument values are passed to a function by object reference.
- If the object is mutable (changeable), the changed value is available outside the function. Mutable objects may be list, set, dictionary, byte array.
- If the object is immutable (not changeable), the changed value is not available outside the function. Immutable objects may be numbers (int, float, complex), strings, tuple, frozen set, bytes.
- If we pass immutable objects to the function, the passing works like pass by value or call by value.
- If we pass mutable objects to the function, the passing works like pass by reference or call by reference.
Examples based on Pass by Object Reference
Example 1:
Let’s write a Python program in which we will pass mutable objects (list) to the function using pass by object reference.
# This program illustrates the concept of call by object reference using mutable objects.
def my_funct(y):
print('Values inside the function before modifying:',y)
y[2] = 4 # modifying value.
print('Values inside the function after modifying:',y)
# Main part of program.
x = [1, 2, 3] # List of values.
print('Values outside the function before modifying:',x)
my_funct(x) # calling function with passing x to function parameter y.
print('Values outside the function after modifying:',x)
Output: Values outside the function before modifying: [1, 2, 3] Values inside the function before modifying: [1, 2, 3] Values inside the function after modifying: [1, 2, 4] Values outside the function after modifying: [1, 2, 4]
In this example, ‘x’ is a list of values. Since the list is a mutable object in Python, the passing acts like call by reference. We have passed x as an argument to the function’s parameter y using call by reference.
Here, y is a formal parameter, whereas x is an actual argument. Both x and y belong to the same memory location. If we make any change in y inside the function, then it also affects in x outside the function.
In this scenario, Python makes only one copy of the argument in the memory location for processing. Values are the same in that copy, but parameter names are different, x and y.
Example 2:
Let’s write a Python program in which we will pass immutable objects (string) to the function using pass by object reference.
# This program illustrates the concept of call by object reference using immutable objects.
def my_func(name):
print('String inside the function before modifying:',name)
name = 'John' # modifying value.
print('String inside the function after modifying:',name)
# Main part of program.
n = 'Herry'
print('String outside the function before modifying:',n)
my_func(n) # calling function with passing n to function parameter name.
print('String outside the function after modifying:',n)
Output: String outside the function before modifying: Herry String inside the function before modifying: Herry String inside the function after modifying: John String outside the function after modifying: Herry
In this example, n is a string. Since string is an immutable object in Python, the passing acts like call by value. We have passed ‘n’ as an argument value to the function’s parameter name.
Here, ‘name’ is a formal parameter, whereas ‘n’ is an actual argument. Initially, both have the same memory location. As we have made an update in the value of name (name = ‘John’), the value of name also has updated.
In this scenario, the value of ‘n’ remains the same and the memory location of ‘n’ also remains the same. But, the memory location of ‘name’ is changed. Hence, Python makes two copies of arguments in two memory locations for processing.
Example 3:
Let’s create a program in which we will pass mutable objects (List) to the function using pass by object reference. In this program, two variables will share the same memory location.
def my_func(y):
print('Id of y before modifying list:',id(y))
print('Values inside the function before modifying:',y)
y[2] = 5
print('Values inside the function after modifying:',y)
# Main program.
x = [1, 2, 3]
print('Values outside the function before modifying:',x)
print('Id of x before modifying list:',id(x))
my_func(x) # function call.
print('Id of x after modifying list:',id(x))
print('Values outside the function after modifying:',x)
Output: Values outside the function before modifying: [1, 2, 3] Id of x before modifying list: 2959160358016 Id of y before modifying list: 2959160358016 Values inside the function before modifying: [1, 2, 3] Values inside the function after modifying: [1, 2, 5] Id of x after modifying list: 2959160358016 Values outside the function after modifying: [1, 2, 5]
In this example, ‘x’ is a list. Since the list is a mutable object, the passing will act like call by reference. We pass ‘x’ as an argument to the function my_func(). ‘x’ is an actual argument, whereas ‘y’ is a formal parameter.
Before updating the list, the memory location of both variable x and y is the same. But, as we made an update in y, also reflected in x. In this case, memory location of both x and y are the same even after making the changing in y.
In this program, we have used id() function that take an object as a parameter (i.e. id(object)) and return the identity of this particular object. The returned value is an integer, which is unique and permanent for this object during its lifetime.
Example 4:
# This program is a demonstration of call by reference overwritten.
def my_func(y):
print('Values inside the function before modifying:',y)
y[1] = 60
print('Values inside the function after modifying:',y)
y = [1, 2, 3, 4, 5] # It would assign a new reference to y.
print('y =',y)
print('x =',x)
# Main program.
x = [10, 20, 30, 40]
print('Values outside the function before modifying:',x)
my_func(x)
print('Values outside the function after modifying:',x)
Output: Values outside the function before modifying: [10, 20, 30, 40] Values inside the function before modifying: [10, 20, 30, 40] Values inside the function after modifying: [10, 60, 30, 40] y = [1, 2, 3, 4, 5] x = [10, 60, 30, 40] Values outside the function after modifying: [10, 60, 30, 40]
In this tutorial, you have learned about the pass by object reference in Python with the help of different examples. I hope that you will have understood the basic concept of call by object reference and practiced all example programs. Stay tuned with the next tutorial where you will understand return statement in Python.
Thanks for reading!!!