PASS BY OBJECT REFERENCE IN PYTHON

def modify(name):
    print("Original Name: ",name)
    print("Original Address of name: ",id(name))
    name="Umesh"
    print("Modified Name: ", name)
    print("Modified Name address: ", id(name))
          
title = "Leo"
print("Title: ", title)
print("Original Address of Title: ",id(title))
modify(title)
print("Name after update: ", title)
print("Memory address of title after update: ",id(title))
          

The output of this code is:

Screenshot_2.png

The same original address of Title and name shows that its not due to "Pass by Value". "Pass by Reference" also don't work in python. Infact python works with "Pass by Object Reference". When we passed value to the function then its object of the value will be passed to the function as argument. In the case of integer, string or value, when you try to update its value then a new variable will be created with a different memory address for the updated value since these data types are immutable( unmodifiable).

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center