In these times of the smoke and mirrors generated by Artificial Intelligence companies and their propagandists, learning programming remains more necessary than ever. In this fourth part of the Python programming course with Linux we will continue explaining how to understand the Object-Oriented Programming paradigm. Remember that the link to the previous lesson is at the end of the post.
Although AI agents and models are capable of understanding and writing code, it is still necessary for a human to be able to give them detailed instructions. of what it has to do. For this, it is not only necessary to understand the problem that the application wants to solve but also to design the solution taking into account the characteristics of the language.
Let's see if we can clarify this with an analogy.
Construction workers build skyscrapers. But before construction begins, architects are needed to design the plans, and engineers to perform the structural calculations and design the electrical, gas, and water connections. Once the building is finished, other people will have to determine the interior design. Of course, experienced workers could probably do a competent job, provided there are no complications. But in practice, nobody wants to take that risk.
The reality is that vibe coding yields better results the more detailed the instructions are And, that large companies have suffered service interruptions and had irreplaceable databases deleted by overly enterprising Artificial Intelligence agents with insufficient instructions.
Python programming course with Linux
We saw in previous articles that the Object-Oriented Programming model is based on 4 pillars: objects, classes, methods, and attributes. Classes are the templates used to model the objects or entities we work with. These templates define the attributes that the objects will have, while methods define the object's behavior.
The different elements created with the template established in the classes are called instances. For example, within the Operating Systems class we have the instances: Linux, Windows, macOS. However, we might want to create classes that are only slightly different from existing classes. It's not necessary to rewrite code because we can take advantage of a feature of Object-Oriented Programming known as inheritance.
Let's look at the following program with an added class.
The reason I don't paste the code directly into the article is, as I already explained, that the platform we use to publish the blog doesn't support indentation. This practice involves adding spaces or tabs (note the "or tabs" since they cannot be mixed) making the code easier to understand.
Indentation makes it easier to structure code hierarchically. Differentiating code blocks such as loops, functions, or classes. Furthermore, if you don't indent your Python programs properly, they won't work.
Now let's analyze the program line by line.
class Sistemas:
As we saw, this program creates the template on which the operating system instances will be created.
def __init__(self, nombre, version, derivada):
Here we are building the class. The first part is an automated method that is used every time one is created. The items in parentheses are the parameters the object will have. `Self` is always used and refers to the object. It always comes first. The other parameters are those we established in previous classes: the name of the Linux distribution, the version, and which distribution it is derived from.
self.nombre = nombre
self.version = version
self.derivada = derivada
These lines are used to create the object's attributes. They are object-specific variables that act as variables within the object and will be retained when needed.
def mostrar_info(self):
This method defines how the object's attribute information will be displayed. The `self` parameter indicates that only parameters saved as attributes should be used.
print(f"Nombre: {self.nombre}")
print(f"Versión: {self.version}")
print(f"Derivada: {self.derivada}")
These 3 lines indicate that the way to display the attribute information is by printing it on the screen.
class SistemaConPeso(Sistemas):
This is where our program differs from the previous one. We've added a new extended class built upon the previous one. That's why the name of the previous class is appended to the class name in parentheses.
def __init__(self, nombre, version, derivada, peso):
This is the constructor of the child class, almost the same as the one we saw before but adding the weight parameter.
super().__init__(nombre, version, derivada)
To avoid repeating code, we tell the program with the super instruction that the task of handling name, version and derivative is the responsibility of the parent class.
self.peso = peso
We still had to take care of saving the weight attribute.
def mostrar_info(self):
Since the goal is to avoid writing extra code, instead of creating a new function that displays the additional data, we will modify the one we already have.
super().mostrar_info()
Call the parent class method to set how the attributes stored in it should be displayed.
print(f"Peso: {self.peso}
Add information about how the data for the current class should be displayed.
sistema = SistemaConPeso("Linux Mint", "22", "Ubuntu", "3GB")
Create the object where the information will be stored with the new parameter added and save it in the system variable.
sistema.mostrar_info()
Call the method responsible for displaying the information
Of course, this program has no real practical uses. It wouldn't make any sense to have to input attribute values ​​in the code, as a programmer would be needed every time something needed to be modified. As we saw in the tic-tac-toe example, Python can receive and store information entered by the user via the keyboard. It can also do this by reading files or interacting with databases.
However, it was useful in clarifying key concepts of object-oriented programming such as classes, attributes, constructors, methods, inheritance, and polymorphism. I promise that in the next article I will finish discussing Object-Oriented Programming and begin talking about how things are done in Python.