Python3: Mutable, Immutable… everything is object!

Daniel felipe Escobar chavez
3 min readMay 22, 2021

Hello programmers friends, today we are going to see how to differentiate two types of data that are mutable and immutable since we will use them throughout the programming in python so let’s start !.

Id:

Let’s start with something basic and try that every time we create an object in python it acquires an id, this happens sirmpe and we can see it here:

>>> py = 87
>>> id(py)
2293405733936
>>>

In addition to an id, it is also important that the data that we create will always have a type either int, string, etc. that will be stored in the object.

>>> type(py)
<class ‘int’>
>>>

mutable objects and immutable objects:

A type is mutable if it is not immutable. A type is immutable if it is a built-in immutable type: str, int, long, bool, float, tuple, and probably a couple others I’m forgetting. User-defined types are always mutable.

An object is mutable if it is not immutable. An object is immutable if it consists, recursively, of only immutable-typed sub-objects. Thus, a tuple of lists is mutable; you cannot replace the elements of the tuple, but you can modify them through the list interface, changing the overall data.

so the mutable objects are:
lists, dict, set

the immutable ones are:
int, float, complex, string, tuple, frozen set

To know and check which is mutable, the only thing we will have to do is try to change its elements like this:

>>> list = [2, 4, 5]
>>> list
[2, 4, 5]
>>> list[2] = 6
>>> list
[2, 4, 6]

this is a mutable type so we can change the data of this list as we want.

>>> tuple = (2, 4, 6)
>>> tuple
(2, 4, 6)
>>> tuple[2] = 6
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: ‘tuple’ object does not support item assignment

as we can see this tuple is of immutable type.

why does it matter and how differently does Python treat mutable and immutable objects?

es importante saber que tipos tenemos dados si son mutables o inmutables pues saber esto nos ayuda cuando queramos trabajar con mutables: para alternar datos cuando trabajamos, o inmutables: para que estos no puedan cambiar.

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

Python Mutable Function Arguments:

when it comes to mutables we can modify the parameters inside the function but this will not modify given outside the function, instead if we modify these parameters outside, it will be done outside the function, to see this in a simpler way here we have this example :

>>> def sum(list=[]):  
... list += [2]
...
>>> lis = [2, 6]
>>> sum(lis)
>>> lis
[2, 6, 2]
>>>

Python Inmutable Function Arguments:

When we create an imutable of the data that we need, it creates a copy in the same function where it is called what causes that when we call it outside it does not cause changes since those changes only occur within the function, as well as the object is a copy It remains unchanged the code block executes it like this. here we can see how it works:

>>> def sum(num):
... num += 5
...
>>> num = 10
>>> sum(num)
>>> num
10
>>>

With this concludes all the explanation of objects, I hope it can be useful. until next time!

--

--