Mutable and immutable objects (py)

Felipe Londoño
Analytics Vidhya

--

In this blog, I'm going to talk about the difference between mutable and immutable objects in python, this topic is really important because if you understand the difference you are going to avoid unexpected behavior when you are modifying different types of data.

Let's start by defining two things, the type of data and the id of a variable, if you had previous experience programming you will know that exist different types of data, like for example, int, float, str, and also every variable that you declare is going to have a unique id.

In the Python interpreter, we can use the function type() to check the type of data of a variable and the function id() to check the unique id of the variable:

>>> x = 10 
>>> type(x)
<class 'int'>
>>> y = 10
>>> type(y)
<class 'float'>
>>> id(x)
10105376
>>> id(y)
13990449964

Mutable objects

The mutable objects are of type list, dict and set this means that these types of data can be modified in multiple ways, let's take as an example the type of data list:

>>> letters = ['a', 'b', 'c']
>>> letters[0] = 'z'
>>> letters[1] = 'y'
>>> letters
['z', 'y', 'c']

In the previous example, we saw how to modify a list using the index but lists have multiple methods like append, extend, join. If you want to learn more about lists and their methods use the following link.

Immutable objects

The immutable on the contrary are all those data types that don't allow any kind of modifications, Some immutable types include int, float, bool, string, tuple.

>>> message = 'Holberton'
>>> message[0] = 'L'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>TypeError: 'str' object does not support item assignment

Python optimizes resources by making two names that refer to the same string value refer to the same object.

>>> message1 = 'Holberton'
>>> message2 = 'Holberton'
>>> message1 == message2

True
>>> message1 is message2
True

Why does it matter treat mutable and immutable objects?

As I was explaining the beginning of this post, is important to handle mutable and immutable objects since we can avoid unexpected behavior and understand how Python works under the hood to optimize the resources

How arguments are passed to functions and what does that imply for mutable and immutable objects

Arguments are always passed to functions by reference in Python. The caller and the function share the same object or variable. When we change the value of a function argument inside the function, the value of that variable also changes inside the caller code block regardless of the name of the argument or variable, this means that when we pass a mutable object can be modified inside another function contrary to immutable objects

>>> def increment(n):
... n += 1
>>> a = 1
>>> increment(a)
>>> print(a)
a = 1 #keeps the same value

By assigning the return of a function to ‘a’ we are reassigning ‘a’ to refer a new object with the new value

>>> def increment(n):
... n += 1
... return n
>>> a = 1
>>> a = increment(a)
>>> print(a)
a = 2 # 'a' refers to a new object with the new value

--

--