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 Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. OOPs is a way of organizing code that uses objects and classes to represent real-world entities and their behavior. In OOPs, object has attributes thing th
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython's input() function
7 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read