Python3: Mutable, Immutable... everything is object!
Jose Alfredo Vallejo Contreras
Developer | Node | TypeScript | Nest | Angular
Introduction:
Object Oriented Programming (OOP) gives us an approach to solving coding problems by making a comparison with our environment, where we can treat or manipulate everything independently. In other words, for our visual or tactile perception, each thing is an object that is sometimes unique in another, the union of many others, cars, chairs, tables, spoons ..., for object-oriented programming this is its main intention, where each piece of code we can treat it independently to finally assemble them all and solve a specific or general problem.
Currently there are several programming languages with this paradigm (OOP) such as C ++, Java, C # ... but I am going to focus on python3 to deal with some terms that are very important to have clear when we start in the world of Object-oriented programming.
The POO is supported in its 4 most important characteristics, which are:
1. Inheritance: allows us to reuse code since we would have a parent class instantiated in many child objects, with common and unique characteristics.
2. Polymorphism: Through this feature it is possible to define various methods or behaviors of an object under the same name, in such a way that it is possible to modify the method parameters, or rewrite its operation, or increase more functionalities to a method.
3. Abstraction: It is the pillar of the OOP, which allows identifying the characteristics and behaviors of an object and with which the class (template) will be built. This means that through this pillar or foundation it is possible to recognize the attributes and methods of an object.
4. Encapsulation: It is the characteristic of the OOP that allows the concealment of the complexity of the code, it belongs to the private part of the class and cannot be seen from any other program.
Each of these characteristics offers different advantages to the OOP that make it the most widely used programming paradigm in the world today. Thanks to its modularity, security and portability, for any novice or professional programmer it is much easier to solve a problem by designing its algorithms so that it can divide a big problem into small pieces of code, I have to assemble them, in this way it can control more Detail the different input / output states that the final application must have, plus another advantage of this type of programming is that by having an application divided into many small pieces of code, we can reuse or migrate code, allowing us to save development time.
id and type
Objects in Python are instances of classes. Classes define the behaviors and properties of each object, for programming terminology we will call the properties "attributes" and the behaviors "methods", each of these has its own properties or variants.
Attributes. These can be specific to the class itself or to the object and are responsible for defining the characteristics of the objects that will be created from the class.
Methods: These are functions that give functionality to our object. The following image shows an example of how attributes and methods interact to define an object.
Mutable and immutable objects
As I mentioned everything in Python is treated as an object and not only the ones that we instantiate through our classes, but the data types themselves are an object, that is, there is a default class for each data type and when we create one variable to store integer data, or any other, a new object is created that will be of the class of the data type associated with the variable.
These predefined data types in different types of classes can be mutable or immutable, this said in other words will be data that can or cannot be modified. An example of immutable data would be: int, str, tuples, and mutable data: list, dict
For example: let's analyze the following code
>>> a = 8 >>> hex(id(a)) '0x94df20' >>> a = 9 >>> hex(id(a))
'0x94df40'
When we created the variable to this it became an alias to an object of type integer and was saved in the memory location 0x94df20, as this data is immutable for Python when we indicate to it in the next line that we need to modify it Python will create another object in another memory location (0x94df40) and will refer the variable "a" to the new object.