Mutable, Immutable… everything is object! (Python)

Img 1 :The standard type hierarchy in Python 3

Python is a language that use the POO paradigm, so that means that everything in Python is an object or a class. Everything can be assigned to a variable or passed as an argument to a function. In another words an object in python is something you can refer to, and you can also assign a value to that object. We can handle everything as object in python.

Things like integers, floats, structures (dictionaries, lists, tuples) and string are object. They all can be stored in variables, passed functions.

Variables are references, something like pointers in C.

Id ( ) and type ( ) functions!

Python has two functions that allows to check what type is the object we are working with and what is it´s ID. Those functions are type () and id () respectively.

We need to know that Python objects has an identity and a class it belongs to. The id of each object is unique during the lifetime in the program and is an integer value. An id is the actual memory address of a variable. Two objects can have the same id; despite they have different life times. An id is given to each object given systematically and randomly. So the id () function returns the identity of the object. This id can be useful to see if we are creating different instances of the classes, or if it is pointing to the same value.

In the next example we can see how is the behavior of Id in python. As all the variables are pointing to the same object, the id is the same for the three.

img 2: Example of id () function

When we need to know the class type of an object we use the type () function as I already said before.

img 3: Example of type () function

Mutable and Immutable Objects!

img 4: Object clasification

Python has two different kind of objects, mutable and immutable. What this implies? If an object is immutable it means cannot be changed. An example of this can be tuples, after we create them, they have to keep the same until the end of the program. We can delete the tuple, but we cannot change the data that already has, that means we cannot add or delete elements inside the tuple. If an object is mutable it means the opposite, it can be changed. An example of this can be the lists. We can add or delete data from this data structure. For example, if inside a tuple, there is a list as one of its elements, this list can be changed, but not the tuple indeed.

Let’s going to see how it works in the next image:

Img 5: Mutable and immutable explanation

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

Mutable and immutable objects are handle in different way by Python, that is why we should know about the difference and know which object belongs to what of the categories

Mutable objects are a good to use when you need to change an attribute value in this object

Immutable objects are more expensive to change because involves creating a copy of the object. Changing mutable objects is cheap because it does not need the creation of a new object and keeps the same ID, as we can see in the next example:

Img 6: Mutable example

And if I try to change a mutable object as tuples, it is going to raise an AtributeError exception, as we can see in the next example:

Img 7: Immutable example

Using object with one or another characteristic depends of the necessity.

In the next image we can see how objects can be classifying in python according to this mutability:

Img 8: Type of objects classification according to its mutability

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

when we pass immutable arguments to a function in Python, this can be changed inside the block of the function, so that means that behave like a copy of the object, so we can say that they are passed by value. When the copy of the object is created, can be manipulated within the scope of the function block.

So the value was only changed inside the block of the function, outside the function will keep the same value.

But when we pass mutable arguments to a function, when those are changed, the content of the parameters changes in everywhere outside the function.

In other words, the value of immutable objects is not changed in the calling block if their value is changed inside the function of method block, but the value of mutable objects is changed.

In the next example we can see how a mutable argument is pass to a function and how its Id keeps the same as we said before.

Img 9: Example argument immutable

That is all for now, thanks for reading.

Hi I am a financial engineer, and a software development student in Holberton School!