Leo Umesh avatar

PASS BY OBJECT REFERENCE IN PYTHON

leoumesh

Published: 13 Aug 2020 › Updated: 13 Aug 2020PASS BY OBJECT REFERENCE IN PYTHON

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).

Leave PASS BY OBJECT REFERENCE IN PYTHON to:

Written by

I am just a passionate blogger.

Read more #hive-148441 posts


Best Posts From Leo Umesh

We have not curated any of leoumesh's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Leo Umesh