How do you pass by 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 is the cause behind 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.

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 paradigms of “Pass by value”, “Pass by Reference” and “Pass by object Reference” can be understood by exploring the below example functions. Look into the two functions defined below:

Python3

def set_list(list):

    list = ["A", "B", "C"]

    return list

def add(list):

    list.append("D")

    return list

my_list = ["E"]

print(set_list(my_list))

print(add(my_list))

Output:

['A', 'B', 'C']
['E', 'D']

Now, let’s explore the above code, 

The variable is not the object

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

 a = ["X", "Y"]

How do you pass by value in python?

What is Pass by Reference In Python?

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

Here, the variable( the bucket) is passed into the function directly. The variable acts as a Package that comes with its contents(the objects).

How do you pass by value in python?

In the above code image both “list” and “my_list” are the same container variable and therefore refer to the exact same object in the memory. Any operation performed by the function on the variable or the object will be directly reflected by the function caller. For instance, the function could completely change the variable’s content, and point it at a completely different object: 

How do you pass by value in python?

Also, the function can reassign the contents of the variable with the same effect as below: 

How do you pass by value in python?

To summarize, in pass-by-reference the function and the caller use the same variable and object.

What is Pass by Value In Python?

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

How do you pass by value in python?

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

How do you pass by value in python?

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

In this Python tutorial, let us discuss on Python pass by reference or value with a few examples.

  • Python pass by reference vs pass by value
  • Python call by reference vs call by value
  • Python pass by reference example
  • Python pass by value example
  • Pass by reference vs value in python
  • Python function arguments pass by reference or value
  • Python pass string by value

Pass by reference – It is used in some programming languages, where values to the argument of the function are passed by reference which means that the address of the variable is passed and then the operation is done on the value stored at these addresses.

Pass by value – It means that the value is directly passed as the value to the argument of the function. Here, the operation is done on the value and then the value is stored at the address. Pass by value is used for a copy of the variable.

Call by reference vs call by value

Call by reference Call by value
While calling a function, in a programming language instead of copying the values of variables, the address of the variables is used, it is known as “Call By Reference.” While calling a function, when we pass values by copying variables, it is known as “Call By Values.”
In this method, a variable itself is passed. A copy of the variable is passed in a call by value.
Change in the variable also affects the value of the variable outside the function. Changes made in a copy of a variable never modify the value of the variable outside the function.
Allows you to make changes in the values of variables by using function calls. Does not allow you to make any changes in the actual variables.
The original value is modified. Original value not modified.

Read: Python NumPy linspace

Python pass by reference example

When we pass something by reference any change we make to the variable inside the function then those changes are reflected to the outside value as well.

Example:

student = {'Jim': 12, 'Anna': 14, 'Preet': 10}
def test(student):
    new = {'Sam':20, 'Steve':21}
    student.update(new)
    print("Inside the function", student)
    return 
test(student)
print("Outside the function:", student)

After writing the above code, Once you will print “student” then the output will appear. Here, we created a dictionary called student, and test(student) is the function. Then two more students joined so we created the variable as “new” and the student.update(new) is used to update the dictionary, also the print will display the output.

You can refer to the below screenshot for python pass by reference example

How do you pass by value in python?
Python pass by reference example

Python pass by value example

When we pass something by value then the changes made to the function or copying of the variable are not reflected back to the calling function.

Example:

student = {'Jim': 12, 'Anna': 14, 'Preet': 10}
def test(student):
    student = {'Sam':20, 'Steve':21}
    print("Inside the function", student)
    return 
test(student)
print("Outside the function:", student)

After writing the above code, Once you will print “student” then the output will appear. Here, we created a dictionary called student, and test(student) is the function. Then two more students joined so we created the variable as “new” and the print will display the output. We can see that the inside and outside function remains the same.

You can refer to the below screenshot for the python pass by value example

How do you pass by value in python?
Python pass by value example

Pass by reference vs value in python

In the below example, we can see that all the parameters in the python language are passed by reference. So, if we change what a parameter refers to within a function, the change also reflects back in the calling function.

Example:

def marks(list):
    list.append([11, 12, 13, 14, 15])
    print("Value inside the function: ", list)
    return
list = [10,20]
marks(list)
print("Value outside the function: ", list)

In this output, we can see that we are maintaining the reference of the passed object, and values are appending in the same object. So, you can see the output of the inside function and outside function.

You can refer to the below screenshot pass by reference vs value in python

How do you pass by value in python?
Pass by reference in python

Python function arguments pass by reference or value

The parameters in the python language are passed by reference. Which mean if we change what parameter refers to within the function, the change also reflect black in the calling function.

Example:

teacher = {'Peter':101, 'John':102, 'Suzain':103}
def test(teacher):
   new = {'kat':104, 'Satya':105}
   teacher.update(new)
   print("Inside the function",teacher)
   return
test(teacher)
print("Outside the function:",teacher)

After writing the above code, Once you will print “teacher” then the output will appear. Here, we created a dictionary called teacher, and the def test(teacher) is the function. Then two more teachers joined so we created the variable as “new” and the print will display the output. We can see that the inside and outside function remains the same.

You can refer to the below screenshot python function arguments pass by reference or value.

How do you pass by value in python?
Python function arguments pass by reference or value

Read: Python NumPy concatenate

Python pass string by value

In this example, we have passed strings to a function, and the string value is an immutable object which is being passed to the function. So, the changes made to the function or copying of the variable are not reflected back to the calling function.

Example:

my_string = "Python"
def test(my_string):
    my_string = "PythonGuides"
    print("Inside the function:",my_string)
test(my_string)
print("Outside the function:",my_string)

In this output, once you will print “my_string” then the output will appear. Here, we created the function called def test(my_string). Here, the passing is like a pass string by the value as we can not change the value of the immutable object.

You can refer to the below screenshot python pass string by value.

How do you pass by value in python?
Python pass string by value

You may like the following Python tutorials:

  • Python select from a list + Examples
  • Python Tkinter Listbox – How to Use
  • Python copy file (Examples)
  • Python File methods (With Useful Examples)
  • Python tkinter messagebox + Examples
  • Union of sets Python + Examples
  • How to convert a String to DateTime in Python
  • Escape sequence in Python

In this Python tutorial, we have learned about the python pass by reference or value. Also, We covered these below topics:

  • Python pass by reference vs pass by value
  • Python call by reference vs call by value
  • Python pass by reference example
  • Python pass by value example
  • Pass by reference vs value in python
  • Python function arguments pass by reference or value
  • Python pass string by value

How do you pass by value in python?

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.

Can you pass by value in Python?

Python uses pass-by-value. It is also true that all variables in Python are references to values, but that doesn't change the fact that the parameter passing is by value. The page itself acknowledges that Java passes by value, and non-primitive types in Java are stored as references.

How do you pass a value by reference in Python?

Pass by reference means that you have to pass the function(reference) to a variable which refers that the variable already exists in memory. Here, the variable( the bucket) is passed into the function directly. The variable acts as a Package that comes with its contents(the objects).

How do you use pass by value?

When you use pass-by-value, the compiler copies the value of an argument in a calling function to a corresponding non-pointer or non-reference parameter in the called function definition. The parameter in the called function is initialized with the value of the passed argument.

How do you pass a variable by value?

Pass By Value: In Pass by value, function is called by directly passing the value of the variable as an argument. So any changes made inside the function does not affect the original value. In Pass by value, parameters passed as an arguments create its own copy.