Pass by Value and Pass by Reference in Python

Passing a value and passing a reference explained below. Below is the simple example which shows how to use these in python.

python pass value reference
Python Pass by Value Vs. Pass by Reference Simple Example

What is Pass by value

What is pass by value? Passing a value as an argument to the function. The pass-by-value concept explained in three steps.

  • Create a function
  • pass value as an argument to the function
  • to verify the result

A literal (20) as an argument supplied here. So it is called pass-by-value.

def my_pass_by_value(b):
    b += 2
    print(b)
c=my_pass_by_value(20)
print(c)
The result:
22
None
** Process exited - Return Code: 0 **
Press Enter to exit terminal

What is Pass by reference

Supplying a reference instead of value is called pass-by-refefence. The working principle you can see in the below example.

  • Create a function
  • pass reference as an argument to the function
  • to verify the result

The below example tells how pass-by-reference works. Value not supplied, instead of the value provided a variable (reference).

def my_pass_by_reference(r):
    r += 10
    print(r)
ref1=100
h=my_pass_by_reference(ref1)
print(h)
The result
110
None
** Process exited - Return Code: 0 **
Press Enter to exit terminal

Recent topics

  • Tony Robbins Vocabulary: 50 Powerful Words and Phrases to Improve Your English

    Tony Robbins Vocabulary: 50 Powerful Words and Phrases to Improve Your English

    Learn 50 powerful Tony Robbins vocabulary words and phrases with simple meanings and example sentences to improve your English speaking skills


  • Migrating JSON Files from AWS S3 to PostgreSQL Using AWS Glue and PySpark

    Migrating JSON Files from AWS S3 to PostgreSQL Using AWS Glue and PySpark

    Migrating JSON files from AWS S3 to PostgreSQL using AWS Glue and PySpark is one of the most common data engineering challenges teams face today. In this guide, you will learn exactly how to build a production-grade AWS Glue JSON to PostgreSQL migration pipeline — from reading raw JSON files in S3 to writing clean,…


  • 5 PySpark Performance Anti-Patterns on Databricks (And How to Fix Them)

    5 PySpark Performance Anti-Patterns on Databricks (And How to Fix Them)

    Databricks makes scaling big data processing feel almost effortless. Under the hood, however, writing PySpark without understanding how Apache Spark and Delta Lake execute your code can quietly ruin performance, spike DBU costs, and cause out-of-memory (OOM) crashes. Below are 5 common PySpark anti-patterns seen in production, along with exact code fixes to keep your…


Discover more from Srinimf

Subscribe now to keep reading and get access to the full archive.

Continue reading