Python Pass By Reference (With Examples)

pass-by-reference-in-python

In Python, there is no concept of pass by value or pass by reference as seen in other programming languages.

Instead, all objects are passed by reference, which means that when an object is passed as an argument to a function, a reference to the original object is passed, rather than a copy of the object.

However, there is a difference in behavior between mutable and immutable objects when they are passed as arguments to a function.

Let’s try to understand what is an object in python programming and what are mutable and immutable objects in python.

What is an Object in python?

In Python, an object is a fundamental concept that represents a piece of data that can be manipulated by a program.

Everything in Python is an object, including numbers, strings, functions, classes, and even the Python interpreter itself.

An object in Python is created from a class, which is a blueprint that defines the properties and methods of the object.

When an object is created, it is given a unique identity that distinguishes it from all other objects.

The identity of an object is its memory address in the computer’s memory.

In Python, there are two kinds of objects.

  • Immutable Objects
  • Mutable Objects

What are Immutable Objects in Python?

Immutable objects are objects whose value cannot be changed after they are created.

Immutable objects cannot be modified once they are created because they do not have any methods that allow them to be modified.

Numbers, strings, and tuples are included in immutable objects.

Here’s an example of an immutable object in Python:

my_string = "Hello, world!"
new_string = my_string.upper()
print(my_string)
print(new_string)

Output

Hello, world!
HELLO, WORLD!

In this example, we created a string called my_string and created a new string called new_string using the upper() method, which returns a new string with all characters in uppercase.

The original string is not modified, and a new string object is created.

What are Mutable Objects in Python?

Mutable objects are objects whose value can be changed after they are created.

If you can change the value of an object once it is created, it is an example of a mutable object.

Lists, dictionaries, and sets are examples of mutable objects.

Pass by Reference in Python

Now, you have understood what are mutable and immutable objects in python.

Let’s try to understand, how an argument is passed through the function.

When an immutable object is passed as an argument to a function, a copy of the object is created and its reference is passed to the function.

Any changes made to the object within the function only affect the copy of the object, and the original object outside the function remains unchanged.

Here’s an example to illustrate the behavior of immutable objects:

def modify_string(my_string):
    my_string = my_string.upper()

a_string = 'hello'
modify_string(a_string)
print(a_string)

Output

hello

In this example, the modify_string() function takes a string my_string as an argument and converts it to uppercase using the upper() method. However, the original string a_string remains unchanged even though it was passed to the function.

On the other hand, mutable objects, such as lists, dictionaries, and objects, can be modified after they are created.

When a mutable object is passed as an argument to a function, a reference to the original object is passed.

Any changes made to the object within the function affect the original object outside the function.

Here’s an example to illustrate the behavior of mutable objects:

def modify_list(my_list):
    my_list.append(4)

a_list = [1, 2, 3]
modify_list(a_list)
print(a_list)

Output

[1, 2, 3, 4]

In this example, the modify_list() function takes a list my_list as an argument and appends the integer value 4 to it.

The original list a_list is modified even though it was passed to the function.

Conclusions: Pass by Reference in Python

While Python does not have the concept of pass by value or pass by reference, there is a difference in behavior between mutable and immutable objects when they are passed as arguments to a function.

Immutable objects are passed by value, while mutable objects are passed by reference.

Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x