Table of contents
1.
Introduction
2.
What is Call By Reference in Python?
2.1.
Python
3.
What is Call By Value in Python?
3.1.
Python
4.
Python Call By Reference or Call By Value
4.1.
Python
5.
Binding Names to Objects
5.1.
Example 1: Variable Identity and Object Equality in Python
5.2.
Python
5.3.
Example 2: List Identity and Object Equality in Python
5.4.
Python
5.5.
Example 3: Immutable Objects and Function Parameter Behavior in Python
5.6.
Python
5.7.
Example 4: Mutable Objects and In-Place Modification in Python Functions
5.8.
Python
6.
Frequently Asked Questions
6.1.
What happens if I try to modify an immutable object inside a function?
6.2.
Can functions change the size of a list passed to them?
6.3.
How can I prevent a function from modifying a mutable object?
7.
Conclusion
Last Updated: May 6, 2024
Easy

Call by Value and Call by Reference in Python

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Python, there are two ways to pass arguments to a function: call by value & call by reference. Understanding the difference between these two methods is crucial for writing effective & efficient code. 

Call by Value and Call by Reference in Python

In this article, we'll learn what call by value & call by reference mean, how they work in Python, & provide examples to show their usage. These methods describe how Python manages the data you provide to functions, which can significantly impact the behavior of your programs.

What is Call By Reference in Python?

In Python, when you pass an argument to a function using call by reference, you are passing the reference or memory address of the object to the function instead of copying the value. This means that any changes made to the object inside the function will affect the original object outside the function.

When you pass a mutable object like a list, dictionary, or a custom object to a function using call by reference, the function can modify the object directly. This is because the function receives the memory address of the object, allowing it to access & manipulate the same object in memory.

For example -: 

  • Python

Python

def modify_list(lst):

   lst.append(3)  # Modifying the list inside the function

my_list = [1, 2]

modify_list(my_list)

print(my_list)
You can also try this code with Online Python Compiler
Run Code

Output

[1, 2, 3]


This will output [1, 2, 3], showing that the list my_list was modified inside the function. Since the list is a mutable object, the function manipulates the same object that my_list references to.

It’s important to understand that this behavior only applies to mutable objects like lists, dictionaries, and sets. For immutable objects (like integers, floats, and tuples), the "reference" behaves as if it were passed by value, because any change creates a new object rather than modifying the old one.

What is Call By Value in Python?

In Python, when you pass an argument to a function using call by value, a copy of the value is passed to the function instead of the reference or memory address. This means that any changes made to the parameter inside the function do not affect the original value outside the function.

When you pass an immutable object like an integer, float, string, or tuple to a function using call by value, a separate copy of the object is created for the function. Any modifications made to this copy inside the function do not affect the original object.

For example : 

  • Python

Python

def modify_number(x):

   x = 10  # This assignment does not affect the original variable

   return x

original_number = 5

new_number = modify_number(original_number)

print("Original:", original_number)

print("New:", new_number) 
You can also try this code with Online Python Compiler
Run Code

Output

Original: 5
New: 10


In this example, original_number remains unchanged after the function call, illustrating how Python treats immutable objects. The variable x inside the function becomes a new integer object, distinct from the one passed in, so original_number remains 5.

It's important to note that in Python, even though the behavior appears to be call by value for immutable objects, it is actually implemented as call by reference. However, since immutable objects cannot be modified, the effect is similar to call by value.

Python Call By Reference or Call By Value

In Python, the concept of call by reference & call by value can be a bit confusing because Python uses a combination of both, depending on the type of object being passed to a function.

When you pass immutable objects like integers, floats, strings, or tuples to a function, Python behaves like call by value. The function receives a copy of the value, & any modifications made to the parameter inside the function do not affect the original object outside the function.

On the other hand, when you pass mutable objects like lists, dictionaries, or custom objects to a function, Python behaves like call by reference. The function receives the reference or memory address of the object, allowing it to modify the object directly. Any changes made to the object inside the function will be reflected outside the function.

However, it's important to note that even when passing mutable objects, if you reassign the parameter to a new object inside the function, it will not affect the original object outside the function. The reassignment only changes the local reference within the function's scope.

For example : 

  • Python

Python

def modify_items(n, lst):

   n = 2  # This will not affect the integer outside the function

   lst.append(4)  # This modifies the list outside the function

num = 1

items = [1, 2, 3]

modify_items(num, items)

print("Number:", num)  # Outputs: Number: 1

print("List:", items) 
You can also try this code with Online Python Compiler
Run Code

Outputs: 

List: [1, 2, 3, 4]


In the above example:

  • The integer num does not change because numbers are immutable in Python and behave as if passed by value.
     
  • The list items changes because lists are mutable and behave as if passed by reference.
     

More Examples : 

Best way to understand this concept is by looking at more examples which will clear every doubt we may have. Now, lets look at those : 

Binding Names to Objects

In Python, variables are names bound to objects. When you pass these variables to functions, what you're really passing is a reference to the object, not the variable itself. This distinction is key to understanding Python's behavior.

Example 1: Variable Identity and Object Equality in Python

  • Python

Python

def update_number(n):

   print("Before update:", id(n))

   n += 1

   print("After update:", id(n))

x = 5

print("Original id:", id(x))

update_number(x)

print("Final value:", x)
You can also try this code with Online Python Compiler
Run Code

Outputs: 

Original id: 138443940674600
Before update: 138443940674600
After update: 138443940674632
Final value: 5


In this example, the id() function is used to demonstrate that the identifier (memory address) of x changes when n is modified within the function. This shows that while x is passed as a reference, any changes to n result in a new object, confirming Python's behavior with immutable objects.

Example 2: List Identity and Object Equality in Python

  • Python

Python

def append_element(lst):
print("List before:", lst, "id:", id(lst))
lst.append(99)
print("List after:", lst, "id:", id(lst))

items = [1, 2, 3]
print("Original list id:", id(items))
append_element(items)
print("Final list:", items)
You can also try this code with Online Python Compiler
Run Code

Outputs: 

Original list id: 137898800333312
List before: [1, 2, 3] id: 137898800333312
List after: [1, 2, 3, 99] id: 137898800333312
Final list: [1, 2, 3, 99]


Here, items retains the same identity throughout the function call, demonstrating that the list object is directly modified. The consistent identifier across different states of items confirms that mutable objects like lists are managed by reference in function calls.

Example 3: Immutable Objects and Function Parameter Behavior in Python

  • Python

Python

def modify_tuple(t):

   print("Tuple before:", t, "id:", id(t))

   t = (4, 5, 6)

   print("Tuple after:", t, "id:", id(t))

my_tuple = (1, 2, 3)

print("Original tuple id:", id(my_tuple))

modify_tuple(my_tuple)

print("Final tuple:", my_tuple)
You can also try this code with Online Python Compiler
Run Code

Outputs: 

Original tuple id: 139528970365632
Tuple before: (1, 2, 3) id: 139528970365632
Tuple after: (4, 5, 6) id: 139528970363904
Final tuple: (1, 2, 3)


The tuple my_tuple does not change after the function call, even though t is reassigned within the function. This behavior reflects how immutable objects are effectively passed by value, as they cannot be altered in place.

Example 4: Mutable Objects and In-Place Modification in Python Functions

  • Python

Python

def update_list(l):

   print("List before modification:", l, "id:", id(l))

   l += [4, 5]

   print("List after modification:", l, "id:", id(l))

my_list = [1, 2, 3]

print("Original list id:", id(my_list))

update_list(my_list)

print("Final list:", my_list)
You can also try this code with Online Python Compiler
Run Code

Outputs: 

Original list id: 135519859589568
List before modification: [1, 2, 3] id: 135519859589568
List after modification: [1, 2, 3, 4, 5] id: 135519859589568
Final list: [1, 2, 3, 4, 5]
a

Frequently Asked Questions

What happens if I try to modify an immutable object inside a function?

When you attempt to modify an immutable object like an integer or a tuple inside a function, Python creates a new object. This means the original object remains unchanged outside the function.

Can functions change the size of a list passed to them?

Yes, functions can change the size and content of a list because lists are mutable. Changes made to a list within a function are reflected outside the function.

How can I prevent a function from modifying a mutable object?

To prevent a function from modifying a mutable object like a list or dictionary, you can pass a copy of the object instead of the original. For lists, you can pass list(original_list) and for dictionaries, use dict(original_dict).

Conclusion

In this article, we have learned about the concepts of call by value and call by reference in python. We discussed how Python treats immutable and mutable objects differently when passed as arguments to functions. With the help of different examples, we showed that immutable types behave as if passed by value, where any function-level modifications result in new objects, leaving the original unaffected. Conversely, mutable types appear to be passed by reference, allowing direct modifications within functions that reflect globally on the original object. 

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass