PASS BY OBJECT REFERENCE IN PYTHON

in GEMS4 years ago
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).

Sort:  

Upvoted by GITPLAIT!

We have a curation trial on Hive.vote. you can earn a passive income by delegating to @gitplait
We share 80 % of the curation rewards with the delegators.


To delegate, use the links or adjust 10HIVE, 20HIVE, 50HIVE, 100HIVE, 200HIVE, 500HIVE, 1,000HIVE, 10,000HIVE, 100,000HIVE


Join the Community and chat with us on Discord let’s solve problems & build together.