As an example of what I am looking to achieve, I have created a Person class that inherits from Speech:
class Habla:
def expresion(frase, grita):
if grita:
return frase.upper()
else:
return frase
class Persona(Habla):
def __init__(self, frase, grita):
self.grita = grita
self.frase = super().expresion(frase, grita)
persona = Persona(frase="Aghh!", grita=True)
The problem I have is that I get an error when executing the method expresion
of the class Speak.
I get the error:TypeError: expresion() takes 2 positional arguments but 3 were given
I suspect that the parameter self
is involved in the matter, but I don't know the cause.
I tried with the debug but I couldn't find when 3 parameters are passed to the method expresion
.
Thanks in advance for any explanation you can give me. I emphasize that my goal is to be able to execute an inherited method inside a class and that I would like to know why I receive this error.
All the best.
I will respond only to the formal consultation.
The code is wrong (does not compile). I corrected it the minimum to answer the question:
produces:
Explanation
The Person class descends from Speech, so it
expresion
becomes its own method.A class's own methods are called using
self
:recommended reading
This question has too many concept and implementation issues. I recommend reviewing Inheritance in Python to see how this type of solution is correctly implemented.
Take the following code as an example and make it always receive "self".
I hope it's worth it. It is important to "play" with self so that Python can reference your classes.
Cheers!