TO BE MUTABLE OR IMMUTABLE

Julioarenas
9 min readMay 27, 2021

--

Introduction according to shakespeare

Well shakespeare is a joke, but to understand the term mutable or immutable, we have to understand what is an object and we can define object as an entity or being independent of its environment, which interacts with other entities or objects, these interact with each other to create something bigger or be part of a system, this object has characteristics and behaviors independent of other objects, but where are these objects? Those objects are stored in certain areas of memory, all objects have characteristics and functionalities, the characteristics we call them attributes and the functions we call them methods, knowing this the only thing left to say is that in python everything is an object and what I mean by this, I mean that even the variables are an object, the functions are an object, the classes are an object etc etc, but I know what is the abstraction of the meaning of an object, that is an entity and so we understand it as humans, but how does it really work in a computer to say that it is an object? For the computer to be able to say that it is an object, it creates three different parts that compose it which are the ID, the TYPE and the Value, the NAME OF THE OBJECT is placed in a global scope that points to the object, that is the NAME is in another memory location to this is assigned a reference of these three parts that are the object, then when we declare a variable these three parts are created and the name points to the object that is to say it points to these three parts, but we do not see this because it goes underneath without us realizing it and you wonder what all this has to do with mutable and immutable is very simple do you remember the parts?
I will explain it better below:

VALUE
When we create an object, we have to assign a value to it so that python allows us to create it and this value means that it has to contain a data of human interpretation or interpretation for other objects, that is to say the data can be a text, a letter, a number or numbers inside it so that the object can be an object.

For example, when creating a variable it is an object in Python and the data we assign to it is its value:

But I know you are asking what is ID and TYPE.

ID
You know when we create an object we create three parts, now let’s see what is an ID. The ID is the numeric identifier of the object and is used to identify the object instead of the name, this is used by the language as a reference for other processes in which the object can be used.

TYPE
Now let’s see the last feature that has an object that is the type, this is very but very linked to the value of the object, in python automatically assigns the type without the programmer having to take care of defining it as in other languages that are strongly typed as the C language with variables, this concept does not make sense in python and less in object-oriented programming, but I mention it because that is where the literal sense of TYPE comes from, but what is TYPE? The type are the main characteristics of the value that will save the object that is to say if what I am going to enter is a text then it is of type string and if what I am going to enter is a number then it will make me of type integer or float or depends on the number that I save in the object, but if what I am going to enter is a type created by the programmer as type employee it means that the object will save information of an employee.

Taking into account all of the above I can explain what is mutable and immutable.

MUTABLE
This means that the value we assign to the object can change during a timeline when the program is running, but all values are not mutable, what does this depend on? It depends on what we made the object for, i.e. we can predefine the object and add its respective value.

THERE ARE THREE TYPES OF MUTABLE OBJECTS:
1. Lists.
2. Sets.
3. Dictionaries.

List Creation Example:

Set Creation Example:

Example of Dictionary Creation:

IMMUTABLE
This means that the value that we assign to the object will never change in its timeline when it is being executed and as in mutable these values depend on what we made the object for, that is to say we can predefine the object and add its respective value.

THERE ARE THREE TYPES OF IMMUTABLE OBJECTS

  1. String.
  2. Numbers.
  3. Tuples.

Example of String creation:

Sets creation example:

Example of dictionary creation:

MUTABLE AND IMMUTABLE RAM MEMORY

When we talk about Python we are talking about objects and objects can be of two types, mutable and immutable, we have to remember what is an object? an object to be an object is composed of ID, VALUE and TYPE, if it does not have these three characteristics is not an object neither more nor less, but what is the difference in memory itself of the mutable and immutable, as we know Python and other languages are built in C, mainly because in C we can manage the memory and in Python from the memory objects are built, but let’s go into detail in Python, the variable NAME is in the global memory of the Ram memory, which is a pointer that points to either static memory or Dynamic memory, while the OBJECT is built in static memory or dynamic memory, since it is allocated memory by means of malloc at run time, but since it works with mutable and immutable, the mutable IS BUILT BY ONE OR MORE OBJECTS, the variable name is pointed to several objects one by one through the reference of its structure that we explained earlier at the beginning of the blog and it is mutable because it can be assigned a new value and it is not that the memory is rewritten but because an object is created in another part of the ram memory and the only thing that happens is that the pointer would no longer be pointing to the previous object if it would not be pointing to the new object created, so in Python retains its name in global memory, but the ID of EVERY OBJECT belongs to the variable that is in the global memory and when changing the object only changes the reference of one of the objects and adds the reference to the new object of the global variable, that is to say in data of compound type as List, Dictionaries and Sets the name preserves the location of the global memory and the different types of data or objects stored in it can change because you add another object in another memory space and the previous object loses the reference of the global variable and the new object is put the reference of the global variable, now let’s talk about the immutables these have a totally different protocol its object id are fixed and do not change so you can not change the object, but then as I change the value of string or an integer happens the same as the immutables another object is created and the pointer would point to this new object, but then what is the difference in memory, the main difference is that the mutables are composite data types that is to say that they are made of one or more objects while the immutables are made of a single object, the mutable ones are mutable because in their internal structure they have objects which can change and the objects cannot modify internally while the immutable ones have only one object that cannot modify internally but they can change another one, that is why they are mutable and immutable.

HOW DOES PYTHON HANDLE IMMUTABLE AND MUTABLE OBJECTS?

Python handles mutable and immutable objects differently, immutable objects are costly for the language because when they need to change their value it is necessary to make a partial copy with the required modification and assign it back to the object but its advantage is that its access is faster than mutable objects, on the other hand mutable objects cost less to change its value because it is not necessary to make a copy but it takes a little time to locate its value.

PASS AS AN ARGUMENT OR AS A REFERENCE IN A FUNCTION?

Immutable objects pass a copy of their value to a function and not the data itself, that is to say
a number, a string and a dupla create a copy of what they are and pass that copy to the function without changing its value of the immutable object and only changes is the copy of that data and not the data itself within the scope of the object.

On the other hand, the mutables change their content within their scope and they would not be giving a copy but the data itself.

I hope it helps you a lot, in case you need something or if I explain it in another way, write me.
Thanks for reading me.

Aliasing

WHAT IS ALIASING?

Well an alias is when we refer to a variable by means of another name but what we care is not the name but the content of the variable, that is to say we refer to the same content by means of another variable that is not the name.
But how does this work in memory?

Very simple when we assign a variable to another one what we are doing is that the two variables point to the same object, that is to say the two global variables will point to the same ID of the same object.

In this case, the state diagram looks like this:

Because the same list has two different names, A and B , we say that it is an alias. Changes made with one alias affect the other:

It is not advisable to use aliases when working with mutable objects. Of course, for immutable objects, there is no problem.

NSMALLPOSINTS and NSMALLNEGINTS

In C numeric data types are assigned a range ie a size to give more precession as to the space that can be set aside in memory, this is done so that a small data is not set aside in a memory space too large if not agree in its ASCII encoding, this is why we have different types of data in C language, but how does this work in Python if it is a language that is not strongly typed, ie the language automatically assigns its size depending on whether it is string or integer or float in memory, let’s see it in more detail.

Well we already know that it is an object in memory ID, VALUE and TYPE, but how do we represent this type if it is not strongly typed simple in Python we use MACROS which predefine the size to store different types of data, for example in Python is done by means of an array of objects if in this case we use a variable and we assign an integer, there will be a predefined array with a size for each object and each of these objects are from -5 to 256 and we do this by means of the macros NSMALLPOSINTS and NSMALLNEGINTS

That is, NSMALLPOSINTS and NSMALLNEGINTS are data types of the variables whose value we assign an integer.

--

--

No responses yet