Pass by Value and Pass by Reference in Python

You might want to punch something after reading ahead, so brace yourself. Python’s argument-passing model is neither “Pass by Value” nor “Pass by Reference” but it is “Pass by Object Reference”. 

Depending on the type of object you pass in the function, the function behaves differently. Immutable objects show “pass by value” whereas mutable objects show “pass by reference”.

You can check the difference between pass-by-value and pass-by-reference in the example below:

Python3




def call_by_value(x):
    x = x * 2
    print("in function value updated to", x)
    return
  
def call_by_reference(list):
    list.append("D")
    print("in function list updated to", list)
    return
  
my_list = ["E"]
num = 6
print("number before=", num)
call_by_value(num)
print("after function num value=", num)
print("list before",my_list)
call_by_reference(my_list)
print("after function list is ",my_list)


Output

number before= 6
in function value updated to 12
after function num value= 6
list before ['E']
in function list updated to ['E', 'D']
after function list is ['E', 'D']

In the above code, we have shown how Python uses call by reference object concept in its program.

We pass an integer in function call_by_value(). Integers are immutable objects hence Python works according to call by value, and the changes made in the function are not reflected outside the function.

We then pass list to function by reference. In function call_by_reference() we pass a list that is an mutable object. Python works according to call by reference in this function and the changes made inside the function can also be seen outside the function.

Pass by reference vs value in Python

Developers jumping into Python programming from other languages like C++ and Java are often confused by the process of passing arguments in Python. The object-centric data model and its treatment of assignment are the causes of the confusion at the fundamental level.

In the article, we will be discussing the concept of how to pass a value by reference in Python and try to understand pass-by-reference examples in Python.

Table of Content

  • Pass by Value and Pass by Reference in Python
  • The variable is not the object
  • What is Pass by Reference In Python?
  • What is Pass by Value In Python?

Similar Reads

Pass by Value and Pass by Reference in Python

You might want to punch something after reading ahead, so brace yourself. Python’s argument-passing model is neither “Pass by Value” nor “Pass by Reference” but it is “Pass by Object Reference”....

The variable is not the object

...

What is Pass by Reference In Python?

Here “a” is a variable that points to a list containing the elements “X” and “Y”. But “a” itself is not a list. Consider “a” to be a bucket that contains the object “X” and “Y”....

What is Pass by Value In Python?

Pass by reference means that you have to pass the function (reference) to a variable, which means that the variable already exists in memory....