Mutable, Immutable… everything is object!

Cristian Fayber Pinzon Capera
5 min readMay 24, 2021

Some curiosities of Python in the assignment of values

Introduction

This high-level language facilitates many processes that can be reduced in a few lines of code. However, when we are talking about assigning values, we must consider that if done in a certain way, can happen unexpected responses, this can be a list, a tuple or even a single variable. We will explain all this in the course of this reading.

id() function

Returns a unique identification code (id) of a object. This is very important since it will allow us to differentiate one object from another, or when it refers to the same. We will see it with a example:

We have made the comparison between the declaration of 2 integer variables and 2 lists initialized with the same values, in the case of integer variables we find that if both contain the same value, their identifier is the same, so we say that it refers to the same object, but in the case of lists even though they have the same values, the identifier is different and we are talking about 2 different objects.

I had forgotten to explain the “is” comparative, which returns me if the 2 comparisons are true or false

type() function

Returns the type of object. With this we can know what type of object we are working with in our programs, it is very important to know it to avoid errors of comparison or assignment of values, in the case of presenting them, make the appropriate adjustments in the code or the corresponding casting.

As it was in the case of id, we will also see an example of its use:

We have tested with 4 different types, in the case of the first 2, even though it has a value of 1, it is evident that the first is an integer and the second is a string, so they are different in type and the resulting comparison is False. For the list and the tuple with equal values they differ in the type, which is also False in their comparison. If we had printed the id of each one, it would be 4 identifiers of different value.

Mutable and Inmutable

In the case of the immutable we refer to the types of variables commonly used, str, int, bool, etc. Where if we modify their value, this is actually saving in another memory space, which implies that the identifier changes. On the other hand with mutables, these can change their value if they lose their address in memory, therefore their id remains intact, as is the case of a list where I can add and remove elements. Let’s look at the following example:

In this code, we have created a function that prints a message if the element is mutable or not, we did the test with a string concatenating another string and a list adding an integer to it, we observed that the first case its id changes after concatenating, therefore we say that it is immutable, but in the case of the list that an integer was added, its id was not modified so we say that it is mutable

Why is it important to know this?

It matters in the sense of knowing how different objects behave when their values are modified, because in this way we will avoid unwanted or unexpected outputs, for example in the case that we overwrite something that “coincidentally” has the same address in memory, although the latter It is not very mentioned in Python and comes from its predecessor which is the C language when it is necessary to allocate space to store our values.

Through the Ids we can predict when an object of our code is replaced or not as our logic, if necessary make the necessary corrections.

Types in Python classified into Immutable and Mutables

Inmutables

  • Strings
  • Int
  • Float
  • Decimal
  • Complex
  • Bool
  • Tuple
  • Frozenset
  • Bytes
  • Range
  • None

Mutables

  • List
  • Dict
  • Set
  • Bytearray
  • MemoryView
  • Defined class

How can I handle this with functions

We are going to show an example of how a list could be copied, where the first function does it in a wrong way and a second one more suitable, we will know why:

We have a function fake_copy_list() that the only thing it does is reassign the content of a list in another variable and return it, but we see that this “copy” has the same id of the list that entered the function, which implies that if b is modified, a would also be modified. In the case of the true_copy_list() function, a list is assigned in a variable, but the difference is that with the list() function a new memory space is created for the list, which returns a new list with the same values entered in function.

To confirm what has been said above, we can print the ids of variables a and b, or also use the comparative is to know if the result is True or False.

You can also perform other similar functions depending on the need for solving the logic of a program, as long as you have if you need to return a mutable or immutable value, using the tools discussed in this reading.

We will see you in a next publication.

--

--