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
  • how do I merge two dictionaries in Python? Python
  • How to remove characters from an imported CSV file Python Tutorial
  • What are measures in Tableau? data visualisation
  • TypeError: ‘NoneType’ object is not iterable class
  • What is a Unique key in SQL? SQL
  • how to use case statements in SQL SQL
  • TypeError: ‘float’ object is not callable Python
  • How to delete a key from a Python dictionary Python

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 Pass Data Between Functions Python Functions
  • TypeError: ‘int’ object is not callable Python
  • How to delete a key from a Python dictionary Python
  • TypeError: the first argument must be callable Python
  • How to Create an XML file from Excel using Python Python
  • YouTube channel lists – Python Lists Python Lists

Select your language!

  • हिंदी
  • Español
  • Português
  • Français
  • Italiano
  • Deutsch
  • What Are Constraints in SQL? SQL
  • IndexError: index 2 is out of bounds for axis 0 with size 2 Index Error
  • How To Validate Cell Values In Excel Python
  • python classes class
  • How to remove characters from an imported CSV file Python Tutorial
  • How To Check For Unwanted Characters Using Loops With Python Python Data Cleansing
  • What does a data analyst do? Livestream
  • How to show percentage differences between files in Python CSV

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