A Beginner’s Guide to Mutable and Immutable Objects in Python

King Ch.
3 min readJul 28, 2023

--

Introduction

In Python, there are two types of objects: mutable and immutable. Mutable objects can be changed after creation, while immutable objects cannot. This difference in mutability has several implications for how Python treats these two types of things.

Id and Type

Every object in Python has a unique id, which is a number that identifies the thing. The id of an object never changes. The type of object is a string that describes the style of the object. The type of an object can change if the thing is changed.

Mutable Objects

Mutable objects are objects that can be changed after they are created. Examples of mutable objects include lists, dictionaries, and sets. Mutable objects can be changed by assigning new values to their attributes or adding or removing elements.

Immutable Objects

Immutable objects are objects that cannot be changed after they are created. Examples of immutable objects include numbers, strings, and tuples. Immutable objects cannot be modified by assigning new values to their attributes or adding or removing elements.

Why Does It Matter, and How Differently Does Python Treat Mutable and Immutable Objects?

The difference in mutability between mutable and immutable objects matters because Python treats these two types of objects differently. For example, when a mutable object is passed to a function, the function can change the object. However, when an immutable object is passed to a function, the process cannot change the object.

Another difference between mutable and immutable objects is how they are represented in memory. Mutable objects are stored in a way that allows them to be changed. Immutable objects are stored in a way that prevents them from being changed.

How Arguments Are Passed to Functions and What Does That Imply for Mutable and Immutable Objects

When an argument is passed to a function, a copy of the argument is made. This means that if the argument is a mutable object, the function cannot change the original object. However, if the argument is an immutable object, the function will be able to change the original object.

This difference in how arguments are passed to functions has implications for mutable and immutable objects. For example, if you pass a mutable object to a function, the function will not be able to change the original object. However, if you pass an immutable object to a function, the function will be able to change the original object.

Conclusion

In this blog post, I have discussed the difference between mutable and immutable objects in Python. I have also discussed how Python treats these two types of objects differently. I hope this blog post has been helpful in understanding mutable and immutable objects in Python.

Code Examples

Python

# This code shows how to create a mutable object (a list) and an immutable object (a string).

mutable_object = [1, 2, 3]

immutable_object = “Hello, world!”

# This code shows how to change the mutable object.

mutable_object[0] = 4

# This code shows that the immutable object cannot be changed.

# immutable_object[0] = “Goodbye, world!”

# This code shows how to pass the mutable object to a function.

def change_list(list_to_change):

list_to_change[0] = 5

change_list(mutable_object)

# This code shows that the mutable object has been changed by the function.

print(mutable_object)

Output

Python

[5, 2, 3]

As you can see, the mutable object has been changed by the function. The immutable object, however, has not been changed by the function.

--

--

King Ch.
King Ch.

No responses yet