Python, mutable and inmutable objects

Python, mutable and inmutable objects

Introduction

Hi everybody, in this blog I'm gonna talk about mutable and inmutable objects in python, what does the functions type() and id(), this because we need them to check differences between the objects, why for python matters each type of object and the implications when you passed as an argument of a function.

Id() and type()

These two python functions allows us to check what our object and compare with other. Id returns the identifier number of the object, similar to C language with the address in memory; in the next image I will show you various examples of this function in the python3 interpreter:

No hay texto alternativo para esta imagen

As you can see I created 3 variables (a, text and list_obj) and pass them as arguments to the function id() that only receive one argument and return the identifier number of the object, this number will be unique during the lifetime of this variable.

Now it's time for the function type(), this function return the type of object you passed as argument, in the next image you can see the result:

No hay texto alternativo para esta imagen

As you can see every object I created has a different type(int, str, tuple, dict).

Mutable and inmutable objects

Python is a Object Oriented language, so each variable has an instance of a object, and one these instances define if an object is mutable or inmutable.

The mutable objects (list, dictionary and set) can change it′s values during the program, inmutable objects (int, float, bool, string, unicode, tuple) can′t change during the program, in the case of mutable objects you will get an error if try to change it; in the next image I will show you this:

No hay texto alternativo para esta imagen

I created different objects, like a string, tuple and integer, and try to change some part of these and as I expected, we get the same error ("TypeError: ... object does not support item assignment), it means that the object is inmutable and you can change it; now, let's try the same process with lists and dictionaries:

No hay texto alternativo para esta imagen

In this case the mutable objects can change their values; that's one of the differences between mutable and inmutable objects, but how is their behaviour in python?

Mutable and Inmutable behaviour

I said that the id of an object is unique, but there are some cases that two or more variables may have the same id when the have the same value, and this only happens with inmutable variables because everytime you create a mutable object it will have a new ID even if it has the same value, the reason of this is for optimization. For example all the variables that have integers between (-5, 256) with other values the ID will be different. Let's see some examples about this:

No hay texto alternativo para esta imagen

All the ids in these variables are the same but as I told you if the number exceeds the range (-5, 256) the id will change ONLY if you assign the number, but if you assign a variable to another the id will be the same.

No hay texto alternativo para esta imagen

Now with mutable objects for every new declaration has it's own id even if the values are the same:

No hay texto alternativo para esta imagen

In this example you can see that even if the lists a and b don't have the same id, they have the same value. That's the second difference between this object.

Behaviour inside functions

Now, one of the most important things about these objects is their behaviour when you passed them through a function. Inmutable objects will keep their value when is passed as an argument to a function, even if it change inside it. With mutable objects their behaviour is totally opposite, if you pass them as argument of a function and inside it is modified, the variable will be modified.

No hay texto alternativo para esta imagen

In this example I create a function that receive an argument, add 1 to it and return the new value. The next step was assign to a variable 'a' the number 8 and pass it through the function, the value that it return was assigned to the variable 'b'. As you can see after 'a' was passed through the function its value don't change, but when the returned value is assigned to 'a' its value change and have a new ID because now it points to a different number.

No hay texto alternativo para esta imagen

Now, check this example with mutable objects. I create a function that assign the word "Bacon" to the first element in a list, the I create a list with 2 elements and pass it through the function, the list after it change its value, and this happen because when a mutable object is passed to a function it keep linked with the extern value, so every change you made to this variable inside of the function will have repercution outside of this.

This is the most important difference between mutable and inmutable objects in Python, I hope their differences are clear.

Cheers!

要查看或添加评论,请登录

社区洞察