Error to access parent class method

Asked

Viewed 144 times

1

Traceback (Most recent call last): File "C:/udemypy/Basico/classes/heranca/ex2/funcionario.py", line 20, in print(gf.getBonificacao()) File "C:/udemypy/Basico/classes/heranca/ex2/funcio.py", line 16, in getBonification Return super(). getBonification + 1000 Attributeerror: 'super' Object has no attribute 'getBonification'

class Funcionario:

    def __init__(self, nome, salario, cpf):
        self.nome = nome
        self.salario = salario
        self.cpf = cpf

   def getBonificacao(self):
            return self.salario * 0.10


class Gerente(Funcionario):

    def getBonificacao(self):
        return super().getBonificacao + 1000


gF = Gerente("weliton", 312.22, "332322333")
print(gF.getBonificacao())
  • If the indentation is as you posted, you set the function getBonificacao within the method __init__ and not as class method. Remove an indentation level from this function. Take advantage, in the class Gerente You also did not make the method call, missed the parentheses there. If these are the errors, I believe the question can be closed due to typo.

  • Even indentation is wrong

  • So now we just need to make the method call, as indicated at the end of the previous comment, right?

1 answer

2


First, indentation is all in Python code. Wrong whitespace and your code will not work: it will give error or generate some unexpected result. In class Funcionario you start using an indentation of 4 blank spaces, then you must maintain constancy for the rest of the program. To define the method getBonificacao you used only 3 spaces and in the body of the method you used 8 spaces. Be coherent: always use 4 spaces.

class Funcionario:

    def __init__(self, nome, salario, cpf):
        self.nome = nome
        self.salario = salario
        self.cpf = cpf

    def getBonificacao(self):
        return self.salario * 0.10

Already in class Gerente, in the body of the method getBonificacao, you did not make the call of the same parent class method, but rather tried to add in 1000 the reference to this method. This does not seem to make sense and to properly call the method it will be necessary to add the parentheses.

class Gerente(Funcionario):

    def getBonificacao(self):
        return super().getBonificacao() + 1000

This way, when running the program, you will get the result 1031.222, which refers to the salary defined on the manufacturer multiplied by 0.10 and added in 1000.

See working on Ideone.

  • my problem is still indentation but I’ll slowly fix these little mistakes Thanks for the help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.