Pass by reference vs value in Python

In Python Call by Value and Call by Reference are two types of generic methods to pass parameters to a function. In the Call-by-value method, the original value cannot be changed, whereas in Call-by-reference, the original value can be changed.

Call by Value in Python

When we pass an argument to a function, it is stored locally (in the stack memory), i.e the scope of these variables lies within the function and these will not affect the values of the global variables (variables outside function).

In Python, "passing by value" is possible only with the immutable types such as integers, floats, strings, and tuples. Even though we pass these as a reference, the object itself cannot be changed.

Example

In the below example code x is an integer (immutable). The num was changed inside the function, but x remains unaffected outside the function ?

def modify_number(num):
    print(f"Original: {num}")
    num += 10
    print(f"Modified inside function: {num}")

x = 5
modify_number(x)
print(f"Outside function: {x}")

The output of the above code is ?

Original: 5
Modified inside function: 15
Outside function: 5

Call by Reference in Python

Unlike in "Call by Value", when we call a method by "passing the reference of a value (object)", the original value will be modified.

The formal and actual arguments are in the same address space. Since we are passing the reference address of the arguments, if we modify a local variable, it is reflected throughout the function.

In Python, we can only pass the reference of mutable types such as lists, dictionaries, and sets, to a function.

Example

In the below example, we are passing the reference of the list object to a function. Since a list is a mutable object, the modifications made inside the function are reflected in the original ?

def modify_list(my_list):
    print(f"Original: {my_list}")
    my_list.append(4)
    print(f"Modified inside function: {my_list}")

my_list = [1, 2, 3]
modify_list(my_list)
print(f"Outside function: {my_list}")

The output of the above code is ?

Original: [1, 2, 3]
Modified inside function: [1, 2, 3, 4]
Outside function: [1, 2, 3, 4]

Pass by Reference vs Value

Some of the key differences between passing a parameter by its value and by its reference are as follows ?

Feature Pass by Value Pass by Reference
Objects Immutable types (integers, strings) Mutable types (lists, dictionaries)
Effect of modification Changes inside the function do not affect the original object outside the function. Changes inside the function directly affect the original object outside the function.
Behaviour It creates a new copy of the object for the function. The function works with the original object reference.
Use Case Only for the types where the values should not change. Only for the types where in-place modification is needed.

Conclusion

Python uses pass-by-value for immutable objects (integers, strings) and pass-by-reference for mutable objects (lists, dictionaries). Understanding this distinction is crucial for writing predictable functions and avoiding unintended side effects.

Updated on: 2026-03-25T07:37:11+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements