Pass by reference vs value in Python
Last Updated :
09 Aug, 2024
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.
Pass by Value and Pass by Reference in Python
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:
Python
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.
The variable is not the object
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".
a = ["X", "Y"]

What is Pass by Reference 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.
Here, the variable( the bucket) is passed into the function directly. The variable acts as a package that comes with its contents (the objects).

In the above code image, both "list" and "my_list" are the same container variable and therefore refer to the same object in the memory. Any operation performed by the function on the variable or the object will be directly reflected by the function caller. For instance, the function could completely change the variable’s content, and point it at a completely different object:

Also, the function can reassign the contents of the variable with the same effect as below:

To summarize, in pass-by-reference, the function and the caller use the same variable and object.
Pass by Reference In Python Example
In this example, the function modify_list takes a list by reference. The function adds the string "Geeks" to the passed list inside the function and prints it. Since lists are mutable data types, the changes made to the list inside the function are also reflected outside the function as you can see in the output.
Python
def modify_list(x):
x.append("Geeks")
print("Inside function:", x)
my_list = ['Geeks', 'for']
modify_list(my_list)
print("Outside function:", my_list)
Output
Inside function: ['Geeks', 'for', 'Geeks']
Outside function: ['Geeks', 'for', 'Geeks']
What is Pass by Value In Python?
In this approach, we pass a copy of the actual variables in the function as a parameter. Hence any modification on parameters inside the function will not reflect in the actual variable.

The same is true for any operation performed by the function on the variable or the object

To summarize, the copies of the variables and the objects in the context of the caller of the function are completely isolated.
Pass by Value In Python Example
Here, we will pass the integer x to the function which is an immutable data type. We then update the value of the integer inside the function and print the updated value. The changes are not seen outside the function as integers are immutable data types.
Python
def modify_integer(x):
x = x + 10
print("Inside function:", x)
x = 5
print("Before function call:", x)
modify_integer(x)
print("After function call:", x)
Output:
Before function call: 5
Inside function: 15
After function call: 5
Python programming uses "pass by reference object" concept while passing values to the functions. This article tries to show you the concept of pass by value and pass by reference in Python. We have shown different cases of passing values with examples. Passing values to a function in Python is different from other coding languages, but with this tutorial, you can easily understand the concept and implement it in your work.
Also Read:
Is Python call by reference or call by value
Similar Reads
Python Dictionary Pass by Value/Reference In Python, dictionaries are passed by reference, not by value. Since dictionaries are mutable, passing them to a function means the original dictionary can be modified.If we want to avoid changes to the original, we can create a copy before passing it. Understanding this behavior is key to managing
3 min read
Is Python call by reference or call by value Python utilizes a system, which is known as "Call by Object Reference" or "Call by assignment". If you pass arguments like whole numbers, strings, or tuples to a function, the passing is like a call-by-value because you can not change the value of the immutable objects being passed to the function.
5 min read
Returning Multiple Values in Python In Python, we can return multiple values from a function. Following are different ways 1) Using Object: This is similar to C/C++ and Java, we can create a class (in C, struct) to hold multiple values and return an object of the class. Python # A Python program to return multiple # values from a meth
4 min read
Use return value in another function - python In Python, one functionâs return value can be used in another, making code cleaner and more modular. This approach simplifies tasks, improves code reuse, and enhances readability. By breaking down logic into smaller functions that share data, you create flexible and maintainable programs. Letâs expl
2 min read
How to use Variables in Python3? Variable is a name for a location in memory. It can be used to hold a value and reference that stored value within a computer program. the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to the variables, you can store
3 min read
Class or Static Variables in Python All objects share class or static variables. An instance or non-static variables are different for different objects (every object has a copy). For example, let a Computer Science Student be represented by a class CSStudent. The class may have a static variable whose value is "cse" for all objects.
9 min read
Pass by Assignment in Python In Python, pass-by-assignment refers to the way function arguments are passed. This means that when a variable is passed to a function, what gets passed is a reference to the object in memory, not the actual object itself. However, whether or not the function can modify the object depends on the mut
2 min read
Scope Resolution in Python | LEGB Rule Here, we will discuss different concepts such as namespace, scope, and LEGB rule in Python. What are Namespaces in Python A python namespace is a container where names are mapped to objects, they are used to avoid confusion in cases where the same names exist in different namespaces. They are creat
5 min read
Global and Local Variables in Python In Python, global variables are declared outside any function and can be accessed anywhere in the program, including inside functions. On the other hand, local variables are created within a function and are only accessible during that functionâs execution. This means local variables exist only insi
7 min read
Python Scope of Variables In Python, variables are the containers for storing data values. Unlike other languages like C/C++/JAVA, Python is not âstatically typedâ. We do not need to declare variables before using them or declare their type. A variable is created the moment we first assign a value to it. Python Scope variabl
5 min read