What is Pass by Value In Python?

In this approach, we pass a copy of the actual variables in the function as a parameter. Hence any modification on parameters inside the function will not reflect in the actual variable.

The same is true for any operation performed by the function on the variable or the object 

To summarize, the copies of the variables and the objects in the context of the caller of the function are completely isolated.

Pass by Value In Python Example

Here, we will pass the integer x to the function which is an immutable data type. We then update the value of the integer inside the function and print the updated value. The changes are not seen outside the function as integers are immutable data types.

Python3




def modify_integer(x):
    x = x + 10  
    print("Inside function:", x)
  
x = 5
print("Before function call:", x)
modify_integer(x)
print("After function call:", x)  


Output:

Before function call: 5
Inside function: 15
After function call: 5

Python programming uses “pass by reference object” concept while passing values to the functions. This article tries to show you the concept of pass by value and pass by reference in Python. We have shown different cases of passing values with examples. Passing values to a function in Python is different from other coding languages, but with this tutorial, you can easily understand the concept and implement it in your work.

Also Read:



Pass by reference vs value in Python

Developers jumping into Python programming from other languages like C++ and Java are often confused by the process of passing arguments in Python. The object-centric data model and its treatment of assignment are the causes of the confusion at the fundamental level.

In the article, we will be discussing the concept of how to pass a value by reference in Python and try to understand pass-by-reference examples in Python.

Table of Content

  • Pass by Value and Pass by Reference in Python
  • The variable is not the object
  • What is Pass by Reference In Python?
  • What is Pass by Value In Python?

Similar Reads

Pass by Value and Pass by Reference in Python

You might want to punch something after reading ahead, so brace yourself. Python’s argument-passing model is neither “Pass by Value” nor “Pass by Reference” but it is “Pass by Object Reference”....

The variable is not the object

...

What is Pass by Reference In Python?

Here “a” is a variable that points to a list containing the elements “X” and “Y”. But “a” itself is not a list. Consider “a” to be a bucket that contains the object “X” and “Y”....

What is Pass by Value In Python?

Pass by reference means that you have to pass the function (reference) to a variable, which means that the variable already exists in memory....