I'm starting in this programming and I'm getting into the OOP. In python, why in the classes it is put __init__
?. And this may sound stupid, but why use classes when you can use functions?
I'm starting in this programming and I'm getting into the OOP. In python, why in the classes it is put __init__
?. And this may sound stupid, but why use classes when you can use functions?
The method
__init__
is a special method and roughly equivalent to what is known as a constructor in other languages. Its main function is to establish an initial state in the object as soon as it is instantiated, that is, to initialize the attributes.Some time ago I made a fairly extensive answer about
__init__
it and its function in Python, I leave you the link so as not to repeat the same thing:What is a constructor?
As for your second question, why not always use structured programming and not complicate our lives with this class thing?
Well, you have to start from the premise of not complicating your life unnecessarily, OOP is not always the best option, it doesn't make much sense if I want to create a function that returns the cube of a number (plain and simple) to start creating a class, with its method, instantiate it, etc.
However, OOP was not created to make us suffer, there are cases where it is very useful and in other cases it is almost essential, such as in the creation of graphical interfaces or video games. There are many benefits and differences of OOP with respect to structured programming, but getting into this is never ending.
In OOP everything revolves around the
objeto
. An object is an entity that has a certain state, behavior (method), and identity. An object in OOP is not very different from a real world "object", for example, a person has a certain state (I am writing this), a behavior (as a person, I can walk, drink, sleep, get bored, write this to avoid boredom XD, etc) and I have an identity (I am me and no one else....).The state of an object (instance) is defined by its
atributos
. They are nothing more than data (variables) that describe the current state of the object.The behavior is specified by the methods . These methods allow the object to do something (walk, write, power on, etc).
Identity is a property of the instance that allows it to be differentiated from any other. An object can always be differentiated, even with respect to objects of the same class.
In structured programming, procedures and data are separated and not related, since the only thing that is sought is the processing of some input data to obtain other output data. The idea is very simple, I have some data and I want to obtain others, for this I create a function, I pass it to them and it returns my new data. In OOP the state (data) is not separated from the methods (processes), the concept of a person does not make sense if we separate their state and methods. If that person is in a coma there are many methods (things that a human being can do for being a human being) that they will not be able to do, it is not the same to walk (method) if I have a broken leg (state) as if not. ..
That is, in OOP we first define an object and then ask it to do something itself.
After this paragraph, a practical example of why the OOP exists. As I mentioned in the creation of graphical user interfaces, this concept is very important. A very common thing in a GUI is a button. We are going to need a lot of buttons, each one in a different way (color, text, action on click, etc). If we need 200 buttons, we can entertain ourselves by creating each one from scratch, each with its own functions, and end up dead trying. The OOP then comes to us like a savior angel and tells us: create a Button class, define its attributes (text, size, color, etc) and the things it can do (click, relaunch, change color, disappear from view, etc). Then you create your 200 buttons by instantiating the class and giving each one its own personality (but still being a button). As a silly example:
The example is very simple but good, let's see if this gets the idea. Now I can create the buttons I want with minimal effort, I just instantiate them:
Now I have two buttons, both different and both do what a button is supposed to do:
The example is simple but close to reality. Note that the button is not limited to executing functions, but that it acts in different ways depending on its state (in this case, whether or not it is disabled). In real implementations this is much more complex, imagine doing this without OOP, you can try...
When you use Python you are using this many times without realizing it. A list is nothing more than a class, it has its attributes (like the content) and its methods (sort, pop, append, etc). Doing
miLista = [1,2,3]
is nothing more than instantiating the classList
and creating an initial state. If wemyLista.append(4)
are using one of its methods, a list can grow and store another element.Continuing with my magnificent analogies to finish, when a human being is born (is instantiated) he does so with a series of characteristics that differentiate him from the rest (attributes), such as hair color, eye color, sex, etc. The method
__init__
does the same thing, as soon as an object is born, it gives it a series of its own characteristics by modifying its attributes. It sounds silly, but this is the OOP way of thinking, and it's very similar to how we structure the real world.Well I'll leave it here, as an object of the class
SerHumano
I execute the methoddejar_de_dar_la_brasa()
and pass my attribute backaburrido
toTrue
... XDClasses is the way to generate objects, OOP is object oriented programming, if you want to use this paradigm you have to think about objects and classes.
a class defines a type of object, when you call it you are creating an instance of that class, when you want something to happen you call that object and tell it to do it.
__init__
is the constructor of your class in which you create a new instance of the class, for exampleWhy use objects when you can use functions, so as not to confuse several similar objects.
Let's imagine a bot that handles 20 customers, you don't want the bot to answer customer 20 a question from customer 18, so you create 20 bot objects and assign each one a customer.
in a game you want to create 100 enemies and each one acts in a similar way, but you want each one to have characteristics that distinguish them, so you create the enemy class and implement the characteristics that distinguish them.
OOP is supposed to have been created when flight simulators started having problems differentiating one plane from another within the simulation.
I am a beginner in programming in general and therefore in python as well.
Beginners ask apparently "dumb" questions because each sign, letter, combination that we see in a line of code, we simply do not understand because everything is new to us.
When I've read such complex answers to such a simple question I realize that, I don't know why, but it's like answering something simple with something simple becomes something complicated.
I think that what this boy wanted to know is what I wanted to know and now he gives me that I have understood it, what I don't understand is why it is not explained directly anywhere and without so much "roll" for what the __init__ works in a practical way, the truth is that it's crazy because it's silly but I only see mega twisted answers in various forums and 5-page internet articles to end up being the same as at the beginning.
For me, a direct answer that explains the use of "__init__" in a class would be something like this:
When you create a class with the intention of making many objects (instances of the class) out of it, which will always have certain characteristics in common, the easiest way to save work in creating each object created using that class is that those objects when you create have default characteristics (properties) and values (arguments), so you don't have to assign those characteristics to the object each time you create an object.
When you use the __init__ function in the class, you are defining the default properties and arguments that the created object will have.
Here I explain it with an example:
If you create the vehicle class and from it you are going to create 2000 vehicles (object cars) and you know that by default most of the vehicle objects that are created with that class will probably have four wheels, they will be SUVs, they will have a diesel engine and ABS, because you define in the class with the __init__ function and those will be the default characteristics that this object will have when creating it using that class, in practice:
(The "self" is basically the object that is created from the class itself, that's why they call it that, I think it would be better understood if the word object were used in this way "object.wheels = wheels")
Now I create the object that I call car_1 from the vehicle class like this:
If I want to know how many wheels that car has, I can print the wheels that the (object) has, 4 will come out:
if I want to know if the car has ABS, (it will come out True):
Because that is what I have defined in the vehicle class using the __init__ function. That is, the objects will be __init__cialized (created) with those parameters.
Now, if now I want to create a car that doesn't have ABS (because I want to see how it slides driving in the rain when braking in a corner) then I create a car (object) and remove the ABS like this:
So now I have a car without ABS (remember that if I say nothing, by default the new object car will have ABS) and I check it like this:
If I want to create a car with an electric motor instead of the default which is diesel, I can create it like this:
And when printing that characteristic of that car, it will come out that it is electric:
It has taken me a week to understand this bullshit because it is not explained anywhere for mortals like me, I must be that I am very stupid or the others are very smart, I don't know.
Calling "def" and not "fun" when creating a function doesn't make things any easier (maybe "fun" is fun and programmers have to look serious).
The "self" thing is also another joke to make it seem like initializing an object in a class is something that only people who work at NASA understand. If instead of calling it "self" they had called it "object", it would be self-explanatory and everyone would understand it without having to explain it...
Anyway, when I finish understanding everything, I will write some book or some tutorial that I will call that will explain simple things in a simple way and not 14-page answers that no god understands.
AMEN