Skip to content
  • YouTube
  • FaceBook
  • Twitter
  • Instagram

Data Analytics Ireland

Data Analytics and Video Tutorials

  • Home
  • Contact
  • About Us
    • Latest
    • Write for us
    • Learn more information about our website
  • Useful Links
  • Glossary
  • All Categories
  • Faq
  • Livestream
  • Toggle search form
  • R – How to check a file exists and is not empty R Programming
  • R – How to open a file R Programming
  • ValueError: pattern contains no capture groups Value Error
  • ValueError: cannot convert float NaN to integer Null values
  • Python Tutorial: How to sort lists Python Lists
  • Python Overview Interview Questions automation
  • What is Apache Spark and what is it used for? Apache Spark
  • hide a column from a data frame Python Dataframe

How to pass by reference or pass by value in Python

Posted on February 12, 2023March 12, 2023 By admin

Estimated reading time: 3 minutes

To start off passing by reference basically means that you are telling the program you have written to pass a variable to a function.

When passing arguments to a function, it is important to understand the meanings first.

As a result, when you pass by reference to a function, the variable data gets processed and changes, but the original value does not change.

On the other hand, when passing by value, you are passing a copy of the data into a piece of your program, and it will not get changed.

It should be noted, as will be shown below, values can change on the original variable, it depends if they are mutable.

Example passing by reference and value – no changes to the original variable

def addition(var):
    var+=1
    print("Argument inside the function:",var)

var = 1
print("Passed by value before the function call,correct as nothing has been actioned on the var.")

print(var) # The code at this point has passed by value, notice the original value has not changed

print("Passed by reference, notice the value has changed, the original var value is copied in.")

addition(var) # This changes the var as a result of the function processing the var argument passed to it.

print("Notice now we have called the function, but it has not changed the original var value.")
print(var)

And the output is as follows:

Output:
Passed by value before the function call,correct as nothing has been actioned on the var.
1

Passed by reference, notice the value has changed, the original var value is copied in.
Argument inside the function: 2

Notice now we have called the function, but it has not changed the original var value.
1

Example passing by reference and value – changes to the original variable

So in the above example, the original values have not been amended. Now we are going to show you an example similar to the above where the value changes.

def addition(var):
    var.append('2')
    print("Argument inside the function:",var)

var = ['1'] # Lists are mutable, hence their values can be updated.
print("Passed by value before the function call,correct as nothing has been actioned on the var.")

print(var) # The code at this point has passed by value, notice the original value has not changed

print("As lists are muteable, changing a list in a function updates the original list as well.")
print("")

print("Passed by reference, notice the value has changed by the function, the original var value is copied in, has also changed.")

addition(var) # This changes the var as a result of the function processing the var argument passed to it.
print("")

print("Notice now we have called the function, but it has changed the original var value, as lists are muteable, they will also get changed outside the function.")
print(var)

And the output is:

Output:
Passed by value before the function call,correct as nothing has been actioned on the var.
['1']

As lists are muteable, changing a list in a function updates the original list as well.

Passed by reference, notice the value has changed by the function, the original var value is copied in, has also changed.

Argument inside the function: ['1', '2']

Notice now we have called the function, but it has changed the original var value, as lists are muteable, they will also get changed outside the function.
['1', '2']

So one lesson to be learned is that when passing by reference or value, there needs to be an understanding of the original data type of the variable being passed.

As a result, this may lead to a number of opportunities as follows:

  1. Using lists, and passing by reference will help to update them when new data comes in that needs to be added.
  2. On the other hand, if you want to ensure that certain pieces of data are immutable, just use a variable that cannot be changed due to its data type.
Python, Python Functions, Python Lists, Python Tutorial Tags:function, immutable, Python

Post navigation

Previous Post: What is Apache Spark and what is it used for?
Next Post: How to use zip() function in Python

Related Posts

  • How To Validate Cell Values In Excel Python
  • Python Tutorial: Add a column to a data frame Python Dataframe
  • How To Run Python Validation From Javascript Javascript
  • How to create a combobox in tkinter Python
  • How to pass multiple lists to a function and compare Python Functions
  • Tkinter python tutorial Python

Select your language!

  • हिंदी
  • Español
  • Português
  • Français
  • Italiano
  • Deutsch
  • Explain different types of data Normalization. SQL
  • How to Generate Random Integers Between 0 and 9 Python
  • How to run Python directly from Javascript Flask
  • How do I fix TypeError: unhashable type: ‘list’ Error? Articles
  • How to Compare Column Headers in CSV to a List in Python CSV
  • How to Automate Testing With Python | unittest automation
  • TypeError: ‘str’ object is not callable Python Functions
  • Regular expressions python Python

Copyright © 2023 Data Analytics Ireland.

Powered by PressBook Premium theme

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT