Reputation: 555
I am working to understand Python and I am currently working on an Employee program. The program is fairly straight forward but I have run into a snag. I am attempting to call one of my other subclass's methods from within a subclass. Both of them are inheriting from the same class but I am not sure what the syntax is. I figured I could demonstrate what I am trying to do without posting all of my code. So here is a shortened version of my program:
class Employee:
num_of_emps = 0
raise_amount = 1
def __init__(self, first, last, id, pay):
self.first = first
self.last = last
self.id = id
self.pay = pay
class Manager(Employee):
raise_amount = 1.08
def __init__(self, first, last, id, pay, emp_under_sup = None):
super().__init__(first, last, id, pay)
if emp_under_sup is None:
self.emp_under_sup = []
else:
self.emp_under_sup = emp_under_sup
def add_emp(self, emp):
if emp not in self.emp_under_sup:
self.emp_under_sup.append(emp)
class Supervisor(Employee):
raise_amount = 1.06
def __init__(self, first, last, id, pay, emp_under_sup = None):
super().__init__(first, last, id, pay)
if emp_under_sup is None:
self.emp_under_sup = []
else:
self.emp_under_sup = emp_under_sup
def add_emp(self, emp):
if emp not in self.emp_under_sup:
self.emp_under_sup.append(emp)
Manager.add_emp(self, emp)
As you can see, in the Supervisor class under the add_emp method I am attempting to call the add_emp method of the Manager class. That way, whenever I create a new employee and add it the Supervisors emp_under_sup list it will also be added to the Manager's emp_under_sup list. The idea being that every employee should be supervised by a Manager without having to explicitly sate it.
In other words if I say:
mng1 = Manager('Jose', 'Federosa', 1, 80000)
sup1 = Supervisor('Jake', 'Derber', 2, 70000)
mng1.add_emp(sup1)
dev1 = Developer('Rob', "M", 3, 60000, 'Python and Java')
sup1.add_emp(dev1)
mng1.info()
Then I want the output to be:
Manager's ID: 1
Full name: Jose Federosa
Salary: 80000
Employee's under supervision: Jake Derber Rob M
However, calling Manager.add_emp(self, emp) from inside the add_emp method of the Supervisor class is not working like I would expect it to. Can someone explain why this won't work?
Upvotes: 0
Views: 164
Reputation: 780724
Give each employee a manager
attribute, and set this in Manager.add_emp()
. Then Supervisor.add_emp()
can get the supervisor's manager, and call add_emp()
on that manager.
class Employee:
num_of_emps = 0
raise_amount = 1
def __init__(self, first, last, id, pay):
self.first = first
self.last = last
self.id = id
self.pay = pay
self.manager = None
def set_manager(self, mgr):
self.manager = mgr
class Manager(Employee):
raise_amount = 1.08
def __init__(self, first, last, id, pay, emp_under_sup = None):
super().__init__(first, last, id, pay)
if emp_under_sup is None:
self.emp_under_sup = []
else:
self.emp_under_sup = emp_under_sup
def add_emp(self, emp):
if emp not in self.emp_under_sup:
self.emp_under_sup.append(emp)
emp.set_manager(self)
class Supervisor(Employee):
raise_amount = 1.06
def __init__(self, first, last, id, pay, emp_under_sup = None):
super().__init__(first, last, id, pay)
if emp_under_sup is None:
self.emp_under_sup = []
else:
self.emp_under_sup = emp_under_sup
def add_emp(self, emp):
if emp not in self.emp_under_sup:
self.emp_under_sup.append(emp)
if self.manager:
self.manager.add_emp(emp)
Upvotes: 1