Mutable or immutable, that is the question!

Andrés Sotelo
6 min readJan 20, 2021
Object Oriented Programming — Python

Have you ever heard of the term “Object Oriented Programming”? Object-oriented programming originated in the 1960s, but became popular in the early 1990s. Its goal is the organization of source code and the reuse of code in similar contexts.

Objects in Python are the same as data in other programming languages. That is, data in Python is represented by objects, by the way they are related and their attributes. We can say then that integers, floats, functions, lists, dictionaries and all data structures are OBJECTS, each with its attributes, characteristics and methods.

id( ) and type( )

Objects in Python have 3 fundamental features: they have an id, a type and a value. In the following image we can see a clear example of this:

Everything is an object

Objects in Python have 3 fundamental features: they have an id, a type and a value.

In the previous image we can see that variable “a” (in Python, unlike languages like C, the variables do not contain the values but point to the objects, so that we understand it more clearly and making an analogy with C, they are like a kind of pointers) refers to the object “5” which is an integer. The integer “5” has a unique ID, which does not change once it is created, that is represented by an integer.

In order to know these characteristics of an object we must use the built in functions type() and id(), which we can apply to the object. Next we have a representation of different types of objects.

Syntax type(object)

On the other hand an object can also belong to a class that has been created. In this case we can see that the object “obj” is an instance of the class “Object”:

An object can be an instance of a created class

Mutable and Immutable objects

We already saw what the objects are in Python, how we can know their type and their ID. At this point it is important to clarify that we can group the objects, so to speak, in two types as far as the mutability of their value.

We can then say that the objects are mutable or immutable. Mutable objects are those that can change their value at any time after being created.

We can then say that the objects are mutable or immutable.

They are mutable objects:
- Lists
- Dictionaries
- Sets
Below we can see an example of this in the following image:

Example of list in Python — Mutable objects

In the previous example we can see a list, whose value has been changed, by changing the index 0. This is in contrast to the immutable objects, which cannot change their value once they have been created. Within these objects we find the following:
- Numbers
- Strings
- Tuples

Next, we can see a string type object with value “Andres”, which cannot be modified:

String — Immutable objects

When trying to change the object’s value, we find that Python gives us an error: TypeError: ‘str’ object does not support item assignment

Is an integer an immutable object?

The answer is yes. An integer is considered an immutable object because its value does not change. When we assign a variable to an integer type object and then change the value of that object, what we are really doing is changing the object, not changing the value of the object. For example: the variable “a” points to object “5”, and then with that object I perform a mathematical operation a = a+1 = 6, the variable “a” is going to point to object 6, which does not mean that object 5 has changed its value.

Let’s look at an example:

Is an integer an immutable object?

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

It is important to know how to handle mutable and immutable objects in Python, as this can prevent us from getting headaches later on.
And it is at this point that we are going to talk about two very important topics that are assignments and references in Python.
Let’s take an example:

Assignments and references in Python

How can it be possible that when changing a variable, not the original one, the original list has been modified? The answer is that when we assign a list to a variable, what we keep in the variable (in this case “lista”) is actually a reference to the list and not the list itself, so, when we copy the variable to another what we did was to copy the reference and not a value. Thus, by changing the list referenced in “lista1” the change is also visible in “lista”.

So that this does not happen, that is to say, so that the changes that we make in a variable do not affect the other variable, we must work making a copy of the value of this variable. This means that both variables refer to two separate objects:

Assignments and references in Python

The situation is very different when working with immutable objects, because the objects cannot be modified in their value. This type of object has another treatment.

Another important thing to emphasize is how Python handles memory for mutated and unchanging objects. The first thing we must clarify is that the immutable objects, once they have been created can be pointed out by different variables without this meaning that new objects are created. On the other hand, mutable objects are stored in different memory positions, even if they have the same value. Let’s see:

Storage of objects in memory — id(object)

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

Another way how Python handles mutable and immutable objects has to do with when they are passed as arguments in functions. Immutable objects are worked on inside functions, but the variable it refers to keeps the same value on the outside.

Another way how Python handles mutable and immutable objects has to do with when they are passed as arguments in functions.

If you liked this post, don’t forget to share it with your friends and I hope it helps you learn more about Python and the world of object oriented programming.
See you in the next one!

--

--