Mutable or Immutable... That is the question
Pilar Andrea Pinto Chaparro
Cloud Consultant | Security | AI | AWS x2 | Ex- AWS | AWS Women Colombia Volunteer
In several programming languages, there is the idea of immutable and mutable objects. But where does this concept come from? Well, this concept has an approach from the mathematical concepts.
We can remember the idea of the constant concept in mathematics, where it refers to a value that does not change. An example of this, are the numbers since they represent a value or quantity. That is, if we observe in the following cases 3, it will always be 3 even though it is added with a 4 or 5. The 3 will always be a 3.
3 + 3 3 + 4 3 + 5
on the other hand, the concept of a variable refers to an unknown value or value that can change. For example, the equation y = 3x + 4 has two variables, x and y. These are variables because you don't know what these values are and these values can change. Your x can equal any number and your y can change depending on your x value.
if x = 1 => y = 7 if x = 2 => y = 10 ...
Changes in the value of X with respect to Y can be expressed in a graph, this shows its change.
Now with this idea in mind, we go to programming languages, since Ada, a language of the 80s, you have the idea of immutable related to the concept of constant, and mutable related to the concept of variable. And since then there is the concept of object-oriented programming, which refers to an abstraction of the real world, we can define an immutable object as an object that does not change its state once it has been created. While a mutable object is able to change its state at runtime, that is, after it has been created.
id and type
You can find the id, a built-in function in Python, that refers to the identity concept and this has to be unique and constant for this object during the lifetime. This is similar to a memory address that can be used by one object at a time while running. And the type is the built-in function that gives the kind of type that an object belongs for. In the next example, you can see the use of these built-in functions.
mutable objects
The mutable objects can change their value in runtime, that is, is a kind of objects that can be changed in-place, without creating a new one to store the updated values. For example, list, dictionary, sets, bytearray, or user-defined classes. Is like the variable concept, you can change an item without affecting the id.
immutable objects
The immutable objects can not change their value after it is created, like the constants, so if it is changed, create a new reference, as seen in the following image. Or is two objects contain the same value then they refer to the same id. The above indicates that an immutable object maintains the address of the value of an object, so it is possible to work with the reference and it is not necessary to create another object. This resembles the concept of aliases, where a new name refers to the same identity of the object to which the alias is put. Examples of these are the integers, floats, strings and tuples
why does it matter
This is relevant because according to one, the type of data that is being used can be taken into account a priori and a program can be better designed, without unexpected results due to whether the data is mutable or immutable. You can also take advantage of the fact that immutable objects cannot change their value, facilitating their use in multi-threaded work without the need to take care of changing the value contained in an object.
how differently does Python treat mutable and immutable objects
Python inside is pure C, so memory management is relevant in this step. For Python it is more expensive to handle mutable objects since as their values change, then you must recreate a copy. While the immutable ones access them more quickly since they maintain their value and simplify it is handled with copies to your reference.
argument functions like mutable and immutable objects
If the arguments are of immutable type, and within the function, a variable is assigned, then it ends up creating a copy that contains the new value, it is like a local duplicate, while the original one remains. On the other hand when an argument is mutable if a dictionary is passed for example and value is changed, then the dictionary that was invoked through the function will also change its value.
References
https://www.pythonforthelab.com/blog/mutable-and-immutable-objects/
https://towardsdatascience.com/python-basics-6-lists-and-list-manipulation-a56be62b1f95
https://www.codementor.io/@sheena/python-lists-in-depth-lrtmk7w4q
https://www.openbookproject.net/thinkcs/python/english2e/ch09.html#objects-and-values
https://www.geeksforgeeks.org/id-function-python/
https://www.wolframalpha.com/input/?i=y+%3D+3x+%2B+4
https://www.oxfordlearnersdictionaries.com/definition/english/immutable
https://study.com/academy/lesson/what-is-a-constant-term-in-math.html