Table of Contents

Mutable vs Immutable Types in Python

In Python, the different data types can be broadly classified into mutable and immutable.

In some languages like C++, there exists 'constants'. However, in Python constants do not exist. Immutable types should not be confused with constants in other languages

Mutable Type

A type is said to be mutable if the data it is handling can be changed or modified. The mutation is a nice way to say that something has changed or modified.

For instance, a list type in Python is mutable. What does this mean? This means that after a list is created its data can be modified.

>>> numbers = [1, 2, 3, 4, 5]
>>> numbers[2]
3
>>> numbers[2] = 10
>>> first_numbers = numbers
>>> first_numbers
[1, 2, 10, 4, 5]
>>> first_numbers.append(15)
>>> first_numbers
[1, 2, 10, 4, 5, 15]
>>> numbers
[1, 2, 10, 4, 5, 15]

In the above example, numbers is a list. We can access the elements in the list (numbers[2]). At the same time, we can also modify the same list (numbers[2] = 10). Then assign a new name for the list, first_numbers. Now both numbers and first_numbers refer to the same list. This is evident in the next steps because as we mutate first_numbers, the numbers also get mutated.

Other mutable types: dict, set

Immutable Type

A type is said to be immutable if it cannot be modified or changed after it is created.

For instance, a string in Python is immutable. What does this mean? This means that the string once created cannot be modified in-place.

>>> name = "Guido"
>>> name[0]
'G'
>>> name[0] = 'K'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> creator = name
>>> creator
'Guido'
>>> creator[0]
'G'
>>> creator[0] = 'K'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> name = name + " van Rossum"
>>> name
'Guido van Rossum'
>>> creator
'Guido'
>>>

In the above example, we see that firstly a string "Guido" is created and it is assigned the label, name. Then we try to modify the string "in place" or mutate it. However, it is not allowed by throwing a TypeError exception.

Then we assign another label, creator to the same string "Guido". We see again that the string cannot be mutated using the new name either.

Finally, we see something that might confuse some people. We see that the data, the label name was referring to is changed. However, this is not a mutation. Rather, Python created a new string "Guido van Rossum" and re-assigned the label name to it. The original string "Guido" is still referred to by the label, creator as seen in the last step.

All primitive types in Python are immutable: None, bool, int, float, str

Other immutable types: tuple

Conclusion

In Python, it is very important to understand the mutability and immutability of types. Sometimes we need data to be modified and sometime we do not. Depending on the use-case, we need to select the right type.

Share this post:

Leave a comment

Similar Posts


Everything is hard before it is easy.