This code has a class and an initialization method. Class objects will have two attributes, called hours and minutes
Horas = valores comprendidos en el rango de 0-23
Minutos = valores comprendidos en el rango de 0-59
My question comes here:
The values for both attributes will be passed in the constructor expression. I do not know if the following code that I attach is correct since I do not understand what the construction expression of the attributes is
class Tiempo:
horas = Time()
minutos = Time()
def __init__(self, horas, minutos):
t1 = Tiempo(6, 30)
t2 = Tiempo(14, 45)
The constructor expression or constructor method refers to a functionality that already exists within the language, in this case in Python it is through
__init__
whose functionalities are:That is, your constructor method is these lines:
Where to pseudovariables:
self.horas
self.minutos
We match them or assign them the value they receive from each of the arguments, that is, if in the instance we pass as values: (10, 20) ; So internally it would look like this:
Where it is indicated that once the class is instantiated it will require 2 values, one to assign it to the property
horas
and the last one for the propertytiempo
.We achieve this when we assign the instance of the class to an object, as follows:
Here to the class we indicate in parentheses the values that it expects to receive
Once the above is done, the class:
miTiempo
Consider that if you follow the syntax that I put at the beginning, then the object expects that you give it 2 values for the instance, otherwise it would give you an error of this type:
If on the other hand you want to give a default value to one of the properties from the class body , then your example should look like this:
Code
Which will give you as a result:
We achieve this by doing:
tiempo
we indicate that it accesses the methodoperTiempo()
to extract the result that the internal logic generates with the values that we gave in the previous line.