Pass by reference vs value in Python
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)
def same_list(list):
return list
my_list = ["X"]
same_list(my_list)
print(my_list)
Output
['X']
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)
def set_list(list):
list = ["A"]
return list
my_list = ["X"]
set_list(my_list)
print(my_list)
Output
['X']
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
def add(list):
list.append("B")
return list
my_list = ["X"]
add(my_list)
print(my_list)
Output
['X', 'B']
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
def fun(lst):
lst.append(4)
print("Inside function:", lst)
a = [1, 2, 3]
fun(a)
print("Outside function:", a)
Output
Inside 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
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
def add(list):
list = ["X", "B"] # reassignment, not in-place modification
return list
my_list = ["X"]
add(my_list)
print(my_list)
Output
['X']
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
def fun(x):
x = x + 10
print("Inside function:", x)
num = 5
fun(num)
print("Outside function:", num)
Output
Inside 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