Python Pass By Object Reference

Python Pass by Object Reference

In the previous chapter, we discussed passing parameters to the functions. We will continue to discuss the same topic by going through the concept of Python Pass by Object Reference in Functions.

Usually, in other programming languages such as Java and C, when the values are passed to a function, they are done in two ways – Pass By Value & Pass By Reference. However, in Python, we don’t follow either of these ways as Python considers everything as an object. It means that the dictionaries, lists, tuples, strings, and even the numbers are considered as objects. That’s why the values are passed in the form of object references.

You can consider any object as a block of memory that stores a particular value. For example, if you add a=20 in your program, the value 20 gets a place first in the memory, and then the variable ‘a’ is mapped to it. And these objects are created based on the size of RAM in your computer. You can use the id() function to know the location of any object in the memory.

#Python program to find the location of object 
a=20 
print("The declared value is",a) 
print("The location of this variable in memory is",id(a)) 

Output

The declared value is 20
The location of this variable in memory is 140166388417360

Find the link to all chapters of our Python learning series here: Learn Python


You can see in the above program that we displayed the declared value first and then displayed its location in the memory. Let us now look at a program to understand the outcome when the declared value is modified.

Python Pass by Object reference for Immutable Integer objects

#Python program to pass an number and modify it
def update(a):
    "To update the value of variable"
    a=25
    print("Inside function")
    print("Number: {0}, Location: {1}".format(a,id(a)))
#Let us call the update function and pass a
a=20
update(a)
print("Outside function")
print("Number: {0}, Location: {1}".format(a,id(a)))

Output

Inside function
Number: 25, Location: 140041255388144
Outside function
Number: 20, Location: 140041255387984

You can see from the program above that the value is updated to 25 only inside the function. And outside, the value is 20 as declared. It means that Python creates another object 25 in the memory, which is also referenced by ‘a’ when the function is called. One of the reasons we create a function in cases like this is because the integer objects are immutable and cannot be modified. So, we change the integer object using a function.

When you pass integer number to function and try modifying it inside the function

Please note that Immutable objects in Python are the objects related to tuples, strings, float and integer. It means that if you try updating any of these objects, a new object with the updated values gets created. However, the objects related to dictionaries and lists are mutable. It means that even if you change their values, the modification occurs in the same object and the creation of a new object is prevented.

Python Pass by Object Reference for Mutable List objects

#Python program to pass a list to a function
def update(list1):
    """to add additional elements to list"""
    list1.append(25)
    print("Inside the function")
    print("The list:",list1)
    print("The location of list: ",id(list1))

#let us call the update() function
list1=[5,6,7,8]
update(list1)
print("Outside the function")
print("The list:",list1)
print("The location of list: ",id(list1))

Output

Inside the function
The list: [5, 6, 7, 8, 25]
The location of list:  140439027648896
Outside the function
The list: [5, 6, 7, 8, 25]
The location of list:  140439027648896
Adding elements to same list through function

After updating the list through the function, you can see that the same list is displayed even outside the function. This works only in cases if you are making minor changes to an existing list. However, if you create a new list object entirely, the value will not get updated outside the function. Let us look at one more concept to understand this concept.

#Python program to pass a list to a function
#and view unchanged data outside the function
def update(list1):
    list1=[20,30,40]
    print("Inside the function")
    print("The list: ",list1)
    print("Location inside function: ",id(list1))
#let us call the function
list1=[11,22,33,44]
update(list1)
print("Outside the function")
print("The list: ",list1)
print("Location outside function: ",id(list1))

Output

Inside the function
The list:  [20, 30, 40]
Location inside function:  140620314138176
Outside the function
The list:  [11, 22, 33, 44]
Location outside function:  140620314061568
When you create new List Object inside the function

Source

Scroll to Top