Using inheritance in python

This is my homework, I saw it earlier on the site, but it seems that it was not resolved, and I got a different error message than the one who asked this question before.

The first part of the problem is to define a subclass of Worker that inherits from Employee and includes an attribute that refers to another employee who is the work manager. You must define the get_manager method that returns the worker manager.

Example:

worker = Worker("Fred", 52000, myboss) 

The second part of the problem is to define an Executive subclass that inherits from Employee and includes an attribute that refers to the annual bonus.

You must redefine the salary method to calculate remuneration based on its salary and bonus. You must use the Employee pay method in determining the pay method for the Executive class.

Example:

 executive = Executive("Kerry", 520000, 1040000) 

My code is written below and an error message appears: "global name" salary "is not defined" in the line "Employee". init ("I", "name, salary") for the Executive class (works for the Worker class). Why am I getting this error and how to fix it?

Thank you for your help!

 class Employee(object): def __init__(self, name, salary): self._name = name self._salary = salary def my_name(self): return self._name def wage(self): return self._salary/26 # fortnight pay class Worker(Employee): def __init__(self, name, salary, manager): Employee.__init__(self, name, salary) self._manager = manager def getManager(self): return self._manager class Executive(Employee): def __init__(self, name, wage, yearlyBonus): Employee.__init__(self, name, salary) self._yearlyBonus = yearlyBonus def wage(self): return Employee.wage(self) 
+4
source share
4 answers

The error is pretty clear. salary not defined in the __init__ Executive method.

You used wage as the __init__ argument, but salary when you called __init__ your parent class, so you have to stick with one variable name:

 class Executive(Employee): def __init__(self, name, salary, yearlyBonus): Employee.__init__(self, name, salary) 

In addition, you can type all of these parameters each time using *args :

 class Executive(Employee): def __init__(self, *args, yearlyBonus): super(Executive, self).__init__(*args) 

Use super() instead of calling the parent class __init__ method. This makes inheritance a bit easier.

+7
source

Just look at the code where the error occurs, and keep looking until you notice that it does not match:

 def __init__(self, name, wage, yearlyBonus): Employee.__init__(self, name, salary) 
+5
source

Yes, your init function does not have a variable named salary, which gives an error when you passed it to an employee. init .

 class Executive(Employee): def __init__(self, name, wage, yearlyBonus): Employee.__init__(self, name, salary) 

when you do it

 executive = Executive("Kerry", 520000, 1040000) 

which correlates with salary? passed this as a member init method. Better if you call the Employee constructor with super

Guessing This Method

 def wage(self): return self._salary/26 

Perhaps this is what you want (not sure how to account for yearlyBonus though)

  class Executive(Employee): def __init__(self, name, wage, yearlyBonus): Employee.__init__(self, name, wage * 26) 
+3
source

Take a look at the Executive.__init__ :

 def __init__(self, name, wage, yearlyBonus): 

Nowhere is there a variable declared salary . But when you call the superclass constructor,

  Employee.__init__(self, name, salary) 

you ask Python to pass the value of a variable named salary as the third parameter. Python just complains that this variable does not exist.

I think you can understand how to fix this from there. :-)

+2
source

All Articles