Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Pass Arguments by Value in Python Function



In Python, when you pass arguments to a function, they are passed by object reference. This means the function gets a reference (or pointer) to the actual object, not a copy of it. However, how this reference affects the object depends on whether the object is mutable or immutable.

  • Mutable objects (like lists, dictionaries, and sets) can be changed inside the function. If you modify a mutable object inside the function, the changes will affect the original object outside the function as well.
  • Immutable objects (like numbers, strings, and tuples) cannot be changed. If you try to modify them inside the function, Python will create a new object, leaving the original object unchanged outside the function.

So, depending on the type of the object, it might seem like you are either passing by value (for immutable objects) or by reference (for mutable objects).

Understanding Python's Argument Passing

Python passes arguments by reference, but this reference is passed by value. This means that if you pass a mutable object (like a list or dictionary), the function can modify the object, but if you pass an immutable object (like integers, strings, or tuples), the function cannot modify the original object.

Passing Immutable Arguments (Passing by Value)

When you pass an immutable object like an integer, string, or tuple, Python behaves as though it is passing by value. This means that any changes made to the argument inside the function do not affect the original variable outside the function.

Example

In the following example, we pass an integer to a function. The function modifies the argument, but the original value outside the function remains unchanged -

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

num = 5
modify_number(num)
print("Outside function:", num)

Following is the output of the above code -

Inside function: 10
Outside function: 5

Passing Mutable Arguments (Passing by Reference)

When you pass a mutable object like a list or dictionary, Python passes the reference to that object. Therefore, modifications made to the object inside the function will affect the original object outside the function.

Example

This example shows how modifying a list inside a function also modifies the list outside the function -

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

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

We get the following output ?

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

Simulating Pass-by-Value for Mutable Arguments

If you want to make sure that changes made inside the function do not affect the original object, you can create a copy of the mutable object before passing it to the function. This simulates pass-by-value behavior for mutable objects.

Example

In the following example, we use the copy() method to create a copy of the list, making sure that the changes inside the function do not affect the original list -

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

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

We get the output as shown below ?

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

Using Immutable Objects for Simulating Pass-by-Value

Since we cannot change immutable objects once created, any operation that seems to modify the value inside a function actually creates a new object. This makes sure that the original object remains unchanged.

Example

In this example, we assign a new string to the variable inside the function, which does not modify the original string outside the function -

def modify_string(s):
   s = "New Value"
   print("Inside function:", s)

original_string = "Old Value"
modify_string(original_string)
print("Outside function:", original_string)

After executing the above code, we get the following output ?

Inside function: New Value
Outside function: Old Value
Updated on: 2025-05-15T12:56:41+05:30

688 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements