What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Pass by Reference in Python | With Examples

  • Oct 07, 2023
  • 7 Minutes Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Abhisek Ganguly
Pass by Reference in Python | With Examples

When it comes to understanding how data is passed between functions and objects in Python, the term "pass by reference" often arises. This concept is essential for any Python programmer to grasp, as it has significant implications for how data is handled and modified within the language. In this article, we will learn about pass-by-reference in Python. Additionally, we will touch upon the role of NumPy arrays in this context.

What is Pass by Reference?

To comprehend the concept of pass by reference, we first need to distinguish it from another common approach known as "pass by value." These two methods describe how data is transferred when passing arguments to functions or manipulating objects.

Pass by Value: In a pass by value system, a copy of the data (such as a variable's value) is passed to a function or assigned to another variable. Any changes made to the copy do not affect the original data.

Pass by Reference: In contrast, pass by reference involves passing a reference to the original data or object rather than a copy. Any changes made to the referenced object inside a function or operation are reflected in the original object.

Let's now look at how Python handles this.

Does Python Pass by Reference?

Python uses a mechanism that is often called "call by object reference" or "pass by assignment." This mechanism can be somewhat confusing because it appears to behave like pass by reference in certain cases, but it is fundamentally different. To understand this better, let's explore some examples.

Immutable Objects in Python

In Python, objects are categorized as either mutable or immutable. Immutable objects, such as integers, strings, and tuples, cannot be modified once created. When you pass an immutable object to a function, you can think of it as "passing by value" because changes within the function do not affect the original object.

def modify_immutable(x):
    x += 10
    print("Inside function:", x)

value = 5
modify_immutable(value)
print("Outside function:", value)

 

Output:

Inside function: 15
Outside function: 5

 

As you can see, the original value outside the function remains unaffected by the modification inside the function.

Mutable Objects in Python

Mutable objects, on the other hand, can be modified. Examples of mutable objects in Python include lists and dictionaries. When you pass a mutable object to a function, it behaves more like "pass by reference" because changes made inside the function are reflected in the original object.

def modify_list(my_list):
    my_list.append(4)
    print("Inside function:", my_list)

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

 

Output:

Inside function: [1, 2, 3, 4]
Outside function: [1, 2, 3, 4]

 

In this case, the changes made to my_list inside the function are visible outside the function as well.

So, the answer to the question "Does Python pass by reference?" is not a straightforward yes or no. It depends on whether the object being passed is mutable or immutable.

How to Pass by Reference in Python

Python does not have explicit support for passing arguments by reference, as seen in languages like C++. However, you can achieve similar behavior by passing mutable objects, such as lists or dictionaries. Here's an example:

def modify_list_ref(my_list):
    my_list.append(5)

my_list = [1, 2, 3]
modify_list_ref(my_list)
print(my_list)

 

Output:

[1,2,3,5]

 

In this example, my_list is passed to the modify_list_ref function, and changes made within the function are reflected in the original list.

Python Pass Object by Reference

In Python, everything is an object, including variables. When you pass a variable to a function, you are effectively passing a reference to the object the variable points to. This behavior holds true for both mutable and immutable objects.

def modify_variable(x):
    x = 10
    print("Inside function:", x)

value = 5
modify_variable(value)
print("Outside function:", value)

 

Output:

Inside function: 10
Outside function: 5

 

In this case, when you reassign x inside the function, it creates a new reference to the integer 10 rather than modifying the original reference.

NumPy Arrays and Pass by Reference

NumPy is a popular library for numerical and scientific computing in Python. When working with NumPy arrays, the behavior is similar to lists for pass by reference.

import numpy as np

def modify_array(arr):
    arr[0] = 99

my_array = np.array([1, 2, 3])
modify_array(my_array)
print(my_array) 

 

Output:

[99,2,3]

 

In this example, changes made inside the modify_array function are reflected in the original NumPy array.

Best Practices for Pass by Reference in Python

Understanding how Python handles pass by reference is essential, but it's equally important to follow best practices to write clean and maintainable code. Here are some recommended practices when working with pass by reference in Python:

Return and Reassign

One of the most straightforward ways to work with pass by reference is to return the altered object from a function and subsequently assign it back to the original variable. This approach clearly indicates that you are making changes to the object and facilitates error checking.

def modify_list_return(original_list):
    modified_list = original_list.copy()  
    modified_list.append(42)
    return modified_list

my_list = [1, 2, 3]
my_list = modify_list_return(my_list)  
print(my_list) 

 

Output:

[1, 2, 3, 42]

 

By returning the modified list and reassigning it, you ensure that changes are explicit and traceable.

Use Object Attributes

When working with classes and objects, it's often better to encapsulate the behavior within the object itself. Define methods within the class that operate on the object's attributes, rather than passing the object to functions.

class ShoppingCart:
    def __init__(self):
        self.items = []

    def add_item(self, item):
        self.items.append(item)

cart = ShoppingCart()
cart.add_item("Product A")
cart.add_item("Product B")

print(cart.items) 

 

Output:

['Product A', 'Product B']

 

By using object attributes and methods, you encapsulate the behavior, making your code more object-oriented and easier to maintain.

Document and Comment

When you have to modify an object in place or work with mutable data structures, it's crucial to document your code clearly. Use comments to explain why you're making the modification and any potential side effects.

def modify_dict_in_place(data_dict):
    """
    Modifies a dictionary in place by adding a 'modified' key.

    Args:
        data_dict (dict): The dictionary to modify.
    """
    data_dict['modified'] = True

my_data = {'value': 42}
modify_dict_in_place(my_data)
print(my_data)  # Output: {'value': 42, 'modified': True}

 

Output:

{'value': 42, 'modified': True}

 

By providing clear documentation, you make it easier for others (and your future self) to understand and maintain the code.

Unit Testing

Consider writing unit tests for functions that modify objects to ensure that they behave as expected. Testing can catch unexpected behavior and help you maintain code quality.

import unittest

class TestModifyList(unittest.TestCase):
    def test_modify_list(self):
        original_list = [1, 2, 3]
        modified_list = modify_list_return(original_list)
        self.assertEqual(modified_list, [1, 2, 3, 42])

if __name__ == '__main__':
    unittest.main()

 

Conclusion

Python's approach to passing arguments may seem like pass by reference for mutable objects but is fundamentally different. Immutable objects are effectively passed by value. Understanding this behavior is crucial when working with Python, as it affects how data is manipulated within functions and methods.

In summary, Python's behavior can be summarized as "pass by object reference," where the reference to the object is passed, and whether the changes affect the original object depends on its mutability. To achieve a more traditional pass by reference behavior, you can pass mutable objects like lists or dictionaries.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Abhisek Ganguly
Passionate machine learning enthusiast with a deep love for computer science, dedicated to pushing the boundaries of AI through academic research and sharing knowledge through teaching.