In Python there is a great variety of concepts that until today we had not had in our lexicon, but when we start this journey of object-oriented programming we must become familiar with them as they are a great basis for our knowledge.
Among the terms that we must know we have the following:
Identity: It never changes and univocally identifies the object. The operator “is” allows us to know if two objects are actually the same. That is, if two variables refer to the same one.
Type: It indicates the type to which it belongs, such as a float or a list. The type() function tells us the type of a given object. It is the class it belongs to.
Value: Every object has particular characteristics. If these characteristics can be modified, we will say that it is a mutable type. Otherwise, it is immutable.
These concepts are the basis for understanding more complex concepts such as mutability and immutability; mutable objects are all those that can be modified once created, the types of mutable objects are:
- And user-defined classes
And immutable are all those that cannot be modified once created, for example:
The way Python handles mutable and immutable objects is very different, in fact it is easier to access the immutable ones since it is a bigger cost to make a copy than to make a direct change in the object as in the mutable objects you can change some value directly in the object.
The arguments in immutable objects are passed by reference as mutable objects, such as list, set and dict. Because of the state of immutable (immutable) objects, if an integer or string value within the function block is changed, then it behaves like an object. copying process. A new duplicate local copy of the calling object is created and manipulated within the scope of the function block. The caller object will remain unchanged. Therefore, the calling block will not notice any changes made within the scope of the function block to the immutable object.
In mutable objects such as dict and list are also passed by reference. If the value of a mutable object is changed within the scope of the function block, then its value is also changed within the scope of the call or main block, regardless of the argument name. Let’s see the following diagram and code example with the explanation at the end when we assign list1 = list2.
Python 3: Sometimes Immutable Is Mutable and Everything Is an Object - Linux.com
What is Python? Python is an interpreted, interactive object-oriented programming language; it incorporated modules…
Mutabilidad en Python
Python es un lenguaje de programación orientado a objetos, y como tal, trata a todos los tipos de datos como objetos…