How to Pass by Reference in Python
- Understanding Pass by Reference vs. Pass by Value
- Method 1: Using Lists to Mimic Pass by Reference
- Method 2: Using Dictionaries for Dynamic Data Manipulation
- Method 3: Custom Classes to Encapsulate Data
- Conclusion
- FAQ

When it comes to programming in Python, understanding how to pass arguments is critical.
This tutorial will walk you through the concept of passing arguments by reference in Python, a topic that often confuses newcomers. While Python does not support traditional pass-by-reference like some other languages, it does allow you to manipulate mutable objects, which can mimic this behavior. By the end of this article, you’ll have a clear grasp of how to achieve this with practical examples, making your coding experience more efficient and effective.
Understanding Pass by Reference vs. Pass by Value
In programming, the terms “pass by reference” and “pass by value” refer to how arguments are passed to functions. In languages that support pass by reference, the function receives a reference to the original variable, allowing it to modify the variable directly. In contrast, pass by value means that a copy of the variable is passed, and changes made within the function do not affect the original variable.
Python uses a model often described as “pass by object reference.” This means that when you pass a variable to a function, you are passing a reference to the object, not the actual variable itself. However, whether you can change the object depends on its mutability. Mutable objects like lists and dictionaries can be modified, while immutable objects like integers and strings cannot.
Method 1: Using Lists to Mimic Pass by Reference
One of the simplest ways to achieve a pass-by-reference-like behavior in Python is by using mutable objects like lists. When you pass a list to a function, any changes made to that list will affect the original list outside the function.
def modify_list(my_list):
my_list.append(4)
my_list[0] = 10
original_list = [1, 2, 3]
modify_list(original_list)
print(original_list)
Output:
[10, 2, 3, 4]
In this example, we define a function modify_list
that takes a list as an argument. Inside the function, we append a new element and modify an existing one. When we call modify_list
with original_list
, the changes reflect in the original list. This demonstrates how mutable objects can be manipulated within functions, effectively mimicking pass by reference behavior.
Method 2: Using Dictionaries for Dynamic Data Manipulation
Dictionaries are another mutable data type in Python that can be used to simulate passing by reference. When you pass a dictionary to a function, any alterations to its contents will also affect the original dictionary.
def update_dict(my_dict):
my_dict['new_key'] = 'new_value'
my_dict['existing_key'] = 'updated_value'
original_dict = {'existing_key': 'old_value'}
update_dict(original_dict)
print(original_dict)
Output:
{'existing_key': 'updated_value', 'new_key': 'new_value'}
In this example, the update_dict
function modifies the original dictionary. It adds a new key-value pair and updates an existing one. The changes are reflected in original_dict
because dictionaries, like lists, are mutable. This allows you to manipulate data structures dynamically, which is particularly useful in more complex applications.
Method 3: Custom Classes to Encapsulate Data
Creating custom classes is another effective way to achieve pass-by-reference-like behavior in Python. By defining a class, you can encapsulate your data and methods, allowing you to modify the object’s attributes inside functions.
class DataContainer:
def __init__(self, value):
self.value = value
def modify_container(container):
container.value += 10
my_container = DataContainer(5)
modify_container(my_container)
print(my_container.value)
Output:
15
In this example, we define a class DataContainer
that holds a value. The function modify_container
takes an instance of this class and modifies its value
attribute. After calling the function, the changes are visible in my_container
, demonstrating how class instances can be used to achieve a pass-by-reference effect.
Conclusion
Understanding how to pass arguments in Python, especially through mutable objects, is essential for effective programming. By leveraging lists, dictionaries, and custom classes, you can manipulate data in a way that feels like pass by reference. This knowledge not only enhances your coding skills but also improves your ability to write clean, efficient, and effective Python code. As you continue to explore Python, remember that mastering these concepts will empower you to tackle more complex programming challenges with confidence.
FAQ
-
What is the difference between pass by reference and pass by value?
Pass by reference allows a function to modify the original variable, while pass by value passes a copy, preventing modifications to the original variable. -
Can you pass immutable objects by reference in Python?
Yes, you can pass immutable objects by reference, but you cannot modify them within the function. You can, however, return a new object. -
What are mutable and immutable objects in Python?
Mutable objects can be changed after creation (like lists and dictionaries), while immutable objects cannot be changed (like strings and tuples). -
How do lists behave when passed to functions?
Lists are mutable, so changes made to a list within a function will affect the original list outside the function. -
Can custom classes be used to mimic pass by reference in Python?
Yes, custom classes can encapsulate data and allow modifications to their attributes, simulating pass by reference behavior.
Lakshay Kapoor is a final year B.Tech Computer Science student at Amity University Noida. He is familiar with programming languages and their real-world applications (Python/R/C++). Deeply interested in the area of Data Sciences and Machine Learning.
LinkedIn