How to pass arguments by reference in a Python function?



In Python, arguments are passed to functions using call by object reference or call by sharing. This means that when you pass a variable to a function, you are actually passing a reference to the object in memory, not a separate copy.

For mutable objects like lists and dictionaries, this reference allows the function to modify the original object directly. So, any changes made inside the function will be reflected outside the function as well, similar to pass-by-reference in other languages.

However, for immutable objects like integers, strings, and tuples, the object itself cannot be changed. If you try to assign a new value inside the function, it simply creates a new object in memory, and the original remains unchanged.

In such cases, you need to return the new value and reassign it outside the function to see the change. So, while Python doesn't support explicit "pass by reference", its behavior depends on whether the object passed is mutable or immutable.

Modifying Mutable Objects Inside Functions

Mutable objects such as lists, dictionaries, and sets can be modified within a function. Since they are passed by reference, any changes made inside the function will also affect the original object outside the function.

Example: List Modification

In this example, we modify a list inside the function and the changes are visible outside -

def add_item(my_list):
   my_list.append("new item")

items = ["apple", "banana"]
add_item(items)
print(items)

The list is modified inside the function because lists are mutable and passed by reference -

['apple', 'banana', 'new item']

Example: Dictionary Update

In the following example, we update a dictionary inside a function -

def update_record(record):
   record["status"] = "complete"

task = {"id": 1, "status": "pending"}
update_record(task)
print(task)

The change inside the function is reflected outside because dictionaries are also mutable -

{'id': 1, 'status': 'complete'}

Immutable Objects Are Not Affected

Immutable objects such as integers, strings, and tuples cannot be changed in place. If you assign a new value to them inside a function, it creates a new object, leaving the original one unchanged outside the function.

Example: Integer Assignment

In this example, we try to modify an integer inside a function, but it doesn't affect the original -

def increment(n):
   n += 1

x = 5
increment(x)
print(x)

Since integers are immutable, modifying n inside the function creates a new local object -

5

Handling Immutable Types by Returning New Values

Since immutable objects like integers and strings can't be changed in place, you can simulate pass-by-reference by returning the modified value from the function. You must then reassign this returned value to update the original variable outside the function.

Example: Return Modified Integer

This example shows how to use return to "update" an immutable value -

def increment(n):
   return n + 1

x = 5
x = increment(x)
print(x)

Here, the updated value is returned and reassigned to the original variable -

6
Updated on: 2025-09-02T13:14:26+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements