Pass by reference vs value in Python
Last Updated :
30 Jun, 2025
In many programming languages like C++ or Java, understanding how arguments are passed to functions, whether by value or by reference, is important. Python, however, follows a unique mechanism that often causes confusion, as it does not strictly follow either pass by value or pass by reference. Instead, Python uses a pass by object reference model, sometimes called call by sharing.
What is Pass by Reference In Python?
In pass by reference, the function receives the memory address of the original object, not a copy. This means both the caller and the function share the same object. With mutable types like lists, dicts and sets, any changes made inside the function will reflect outside as well.
Example 1: No Change (Same Reference, No Modification)
Python
def same_list(list):
return list
my_list = ["X"]
same_list(my_list)
print(my_list)

Explanation: Both list inside the function and my_list outside point to the same list in memory. Since the function didn’t modify anything, the list remains unchanged.
Example 2: Reassignment (New Object Inside Function)
Python
def set_list(list):
list = ["A"]
return list
my_list = ["X"]
set_list(my_list)
print(my_list)
Explanation: Function rebinds list to a new list object. This does not affect my_list. list now refers to a new object inside the function and my_list still refers to the original object, which remains unchanged.
Example 3: In-Place Modification
Python
def add(list):
list.append("B")
return list
my_list = ["X"]
add(my_list)
print(my_list)

Explanation: Function modifies the list in-place. The change is visible outside. Since list.append("B") changes the contents of the list in place, my_list is also modified.
Example 4: Mutating a List in a Function
Python
def fun(lst):
lst.append(4)
print("Inside function:", lst)
a = [1, 2, 3]
fun(a)
print("Outside function:", a)
OutputInside function: [1, 2, 3, 4]
Outside function: [1, 2, 3, 4]
Explanation: List is mutated inside the function and the changes persist outside because both lst and a refer to the same object.
What is Pass by Value In Python?
In pass-by-value, a copy of the variable is passed, so changes inside the function don't affect the original. While Python doesn't strictly follow this model, immutable objects like int, str, and tuple behave similarly, as changes create new objects rather than modifying the original.
Example 1: Same Reference, No Change
Python
def same_list(list):
return list
my_list = ["X"]
same_list(my_list)
print(my_list)

Explanation: Although both my_list and list point to the same object, no changes were made. So the object remains exactly the same.
Example 2: Reassignment with Immutable behavior
Python
def add(list):
list = ["X", "B"] # reassignment, not in-place modification
return list
my_list = ["X"]
add(my_list)
print(my_list)

Explanation: Inside the function, list is reassigned to a new object. But this does not change my_list outside the function.
Example 3: Immutable Integer
Python
def fun(x):
x = x + 10
print("Inside function:", x)
num = 5
fun(num)
print("Outside function:", num)
OutputInside function: 15
Outside function: 5
Explanation: Integers are immutable, so modifying x inside the function creates a new object. The original num remains unchanged.
Pass by Reference vs Pass by Value
Aspect | Pass by Reference | Pass by Value |
---|
Object Type | Mutable (list, dict, etc.) | Immutable (int, str, etc.) |
---|
What is Passed | Reference to object | Reference to object |
---|
Can Modify in Function? | Yes (affects original) | No (new object created) |
---|
Affects Original? | Yes | No |
---|
Typical Behavior | Like aliasing | Like copying |
---|
Related articles
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
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
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
Get Variable Name As String In Python Getting a variable name as a string in Python means finding the label that points to a specific value. For example, if x = "Python", we can loop through globals() or locals() and compare values using the is operator to find that the name x points to "Python". Letâs explore some simple and effective
2 min read
Get Variable Name As String In Python Getting a variable name as a string in Python means finding the label that points to a specific value. For example, if x = "Python", we can loop through globals() or locals() and compare values using the is operator to find that the name x points to "Python". Letâs explore some simple and effective
2 min read