In Python programming, understanding how functions receive and handle arguments is essential. Many developers, especially those with experience in other languages, often question whether Python uses call by value, call by reference, a combination of both, or a different model entirely. This distinction is important because it affects whether changes made inside a function impact the original data outside the function. In this blog, you will understand what call by value and call by reference mean, how Python handles mutable and immutable data during function calls, along with detailed examples.
Table of Contents:
What is Call by Value in Python?
Call by value refers to a scenario where a function receives a copy of the argument’s value, not the original variable itself. In this method, alterations in the parameter within the function do not impact the original variable outside the function. While Python does not implement call by value in the same way as C, immutable types like integers, floats, and strings follow the same pattern. Any change made to these inside a function creates a new object, leaving the original data unchanged. This behavior resembles call-by-value, even though Python does not follow it in the traditional way.
What is Call by Reference in Python?
Call by reference is the method of calling functions in which the reference to the original variable is passed and not a copy of its value. This enables the function to change the real data in the memory. Although Python does not implement call-by-reference using pointers like C++, it exhibits reference-like behavior when mutable objects (such as lists, dictionaries, or user-defined objects) are passed as arguments. Any change made to such an object inside the function is also seen outside the function. This occurs because both the original variable and the function parameter refer to the same object in memory.
Unlock Python: Power Up Your Programming Skills!
Dive into real-world coding projects and become confident in writing clean, efficient Python code.
Is Python Call by Value or Call by Reference?
Python uses call by object reference (also called call by sharing), where a function receives a reference to the original object in memory. However, the reference itself is passed by value, meaning the function gets a copy of the reference, not the actual variable. Because of this, the function works directly with the same object, so any modifications to mutable objects (like lists or dictionaries) inside the function will affect the original object outside. On the other hand, if the function reassigns the parameter to a new object, this reassignment only changes the local reference within the function and does not affect the original variable outside. This model helps explain why Python behaves differently from traditional call-by-value or call-by-reference approaches used in other programming languages.
Examples of Call by Value and Call by Reference in Python
To identify the behaviour of the function arguments in Python, we need to make a distinction between the immutable and mutable data types. The two examples below help illustrate how Python manages object references during function calls.
Example 1: Immutable Types (int, str)
In this method, immutable types such as integers and strings are passed to functions. Any change to these objects inside the function results in a new object being created rather than modifying the original.
Example:
Output:
Explanation: Here, the function is passed an integer 5. Integers, in Python, are immutable, and the addition of x + 10 in the scope of the function just creates a new integer object. The call by value behavior is evident since the original variable number remains unchanged after the function call.
Example 2: Mutable Types (lists, dicts)
In this approach, the mutability of objects such as lists and dictionaries is presented as the technique that enables changes to be made in-place, with the changes being visible outside of the scope of the function.
Example:
Output:
Explanation: Here, the list [1, 2, 3] is provided to the function. Since lists are mutable, the append() method modifies the original list in place. As a result, the change is visible even after the function call, beyond the scope of the function, and it seems like call-by-reference performance.
Get 100% Hike!
Master Most in Demand Skills Now!
Binding Names to Objects in Python
In Python, variable names are simply references (bindings) to objects in memory. When you pass a variable to a function:
- A new name (parameter) is created within the function’s local scope.
- This new name refers to the same object in memory as the original variable.
- Because both names point to the same object, changes to mutable objects inside the function will affect the original object outside the function.
- However, if the parameter is reassigned to a new object inside the function, it only changes the local reference and does not affect the original variable outside.
- This explains why modifying mutable objects inside functions changes the original, but assigning a new object only changes the local reference.
Difference Between Call by Value and Call by Reference in Python
Feature |
Call by Value |
Call by Object Reference |
Data Passed |
A copy of the actual value is passed |
A reference to the object is passed by value |
Can Modify Data |
No, changes inside the function do not affect the original |
Yes, if the object is mutable |
Effect on Caller |
Original variable remains unchanged |
Mutable objects can be changed, immutables cannot |
Use Case |
Used to prevent unintended side effects |
Allows modifying immutable objects without copying |
Works with Mutable Types |
A copy of the value is modified without affecting the original |
Changes made to the object persist outside the function |
Works with Immutable Types |
Original variable remains unchanged |
Rebinding creates a new object, original stays unchanged |
Common Mistakes with Call by Value and Call by Reference in Python
These are some of the most common mistakes developers make when passing arguments to functions in Python, especially due to misunderstandings about call by value and call by reference.
Mistake 1: Assuming Python Uses Traditional Call by Reference or Call by Value
Many beginners assume Python uses classic call by value or call by reference, like other languages. In reality, Python passes references to objects, not copies of the data. The reference itself is passed by value, so mutable objects can be changed inside functions. This difference often causes confusion and debugging issues.
Example:
Output:
How to Fix: Know that Python employs a call by object reference, commonly referred to as the call by assignment mechanism. It implies that those are references to actual objects, and binding them within the function does not have any influence on the original object in the case of immutable objects such as int, str, or tuple.
Corrected Code:
Output:
Mistake 2: Modifying Immutable Objects Expecting Changes to Persist
Some developers often assume that modifying an immutable object inside a function will also change it outside the function.
Example:
Output:
How to Fix: Understand that strings are immutable in Python. When modified, a new string object is created inside the function, and the original one remains unchanged. If you need to return the modified version, do so explicitly.
Corrected Code:
Output:
Mistake 3: Not Realizing Mutations on Mutable Objects Affect Original Values
This issue is the opposite of the previous mistake. Here, developers expect changes to mutable objects inside a function not to affect the original object, when in fact, they do. Many programmers unintentionally alter lists or dictionaries inside a function, expecting the original object to remain unchanged.
Example:
Output:
How to Fix: Recognize that mutable objects like lists and dictionaries will reflect changes made inside the function. If you don’t want that, make a copy before modifying.
Corrected Code:
Output:
How Memory Model Affects Function Arguments in Python
To understand how arguments are handled in Python, it’s important to know how Python manages variables in memory. Unlike languages like C++ or Java, Python does not pass the actual value or a direct reference. Instead, Python uses call by object reference (also called call by sharing or call by assignment). This means that both the parameter inside the function and the argument outside point to the same object in memory, but the reference itself is passed as a copy. Whether changes inside the function affect the original data depends on whether the object is mutable or immutable.
Learn Python for Free: Start Coding Smarter Today!
Kickstart your programming journey with hands-on Python lessons at no cost.
Conclusion
Understanding how Python handles function arguments is essential for writing clean and reliable code. Python uses a model known as call by object reference or call by assignment, where functions receive a reference to the object, but the reference itself is passed by value. As a result, changes made to mutable objects within a function affect the original data, while reassignments to immutable objects do not alter the original variable. This difference helps you understand how your code works and prevents unexpected problems.
Take your skills to the next level by enrolling in the Python Course today and gaining hands-on experience. Also, prepare for job interviews with Python Interview Questions prepared by industry experts.
Call by Value and Call by Reference in Python – FAQs
Q1. Is Python call by value or call by reference?
Python uses call by object reference, so functions receive a reference to the object. Mutable changes affect the original, but reassigning immutable objects doesn’t change the original.
Q2. And why does a modification of a variable within a function do not affect the original variable?
If a variable points to an immutable object like an int, str, or tuple, any change inside a function creates a new object. The original variable stays unchanged because the function’s parameter now refers to a different object.
Q3. Is it possible to modify the original list sent as an argument to a function or a dictionary?
Yes. Lists and dictionaries are mutable items, and any modification in-place, such as addition, erasing, or modifying an element, will change the object when it is outside the scope of the function.
Q4. What do I do to prevent a mutable argument from being changed by a function?
In order not to modify the mutable object, and pass a copy of the object rather (e.g., list.copy() or dict.copy()), operate with that copy within the function.
Q5. Why does python appear to be call by value and call by reference?
In Python, immutable types act like call by value, while mutable types act like call by reference. This is due to Python’s call by object reference model, where behavior depends on mutability.