Mutable or Immutable...  That is the question

Mutable or Immutable... That is the question

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.

No hay texto alternativo para esta imagen

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.

No hay texto alternativo para esta imagen

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.

No hay texto alternativo para esta imagen


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

No hay texto alternativo para esta imagen

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://learnandlearn.com/python-programming/python-how-to/python-function-arguments-mutable-and-immutable

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://towardsdatascience.com/https-towardsdatascience-com-python-basics-mutable-vs-immutable-objects-829a0cb1530a

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





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

Pilar Andrea Pinto Chaparro的更多文章

  • A new paradigm based on objects

    A new paradigm based on objects

    Object-oriented programming is the basis on which python was built, one of the most widely used languages today. This…

  • C -libraries

    C -libraries

    In the programming and in general engineering world is important to do the things and the processes in an efficient…

    1 条评论
  • Negative numbers in computers

    Negative numbers in computers

    In the current days, computers are the main part of our lives in that way that we usually do not have an idea of how it…

  • C static libraries

    C static libraries

    A library is a set of variables and functions that can be used for a lot of programs, just using a header files for…

  • What happens when you type gcc main.c

    What happens when you type gcc main.c

    To introduce this topic, we have to clarify that it is C, it is one of the most relevant languages that exist in the…

  • What is the difference between a hard link and a symbolic link?

    What is the difference between a hard link and a symbolic link?

    In linux, the use of a shell in order to make some actions with some files, like open, remove, paste, create, and a lot…

  • What happens when you type ls *.c

    What happens when you type ls *.c

    For start this discussion we have to know Linux, Linux is an open source operative system that is commonly used with…

社区洞察

其他会员也浏览了