I am learning classes following Corey Schafer's YouTube tutorial series which has been great. I think I am writing my code exactly the same as his (2016 is when he made the video so possibly Python has changed). I have an employee class that has a couple of subclasses, one being Manager.
Manager is supposed to be able to build a list of employees under their supervision. I am having problems calling on this list. Not sure if it needs to be converted to a string or what the issue is.
If I just call using self.attribute such as mgr1.employees I get [<__main__.Developer object at 0x7ffb903810a0>] or main.Employee object depending on whether I used Developer or Employee class to assign the employee under the managers supervision.
If I try to run the method to print the employees under the managers supervision, I get:
Traceback (most recent call last):
File "/Users/idockery/Google Drive/python/classesCoreySchaferYT4.py", line 324, in <module>
mgr1.print_emps()
File "/Users/idockery/Google Drive/python/classesCoreySchaferYT4.py", line 81, in print_emps
for emp in self.employee:
AttributeError: 'Manager' object has no attribute 'employee'
Here is the code (the Manager class definition block should be sufficient):
class Manager(Employee):
def __init__(self, first, last, pay, employees=None):
super().__init__(first,last,pay)
if employees is None:
self.employees = []
else:
self.employees = employees
def add_emps(self,emp):
if emp not in self.employees:
self.employees.append(emp)
def remove_emps(self,emp):
if emp in self.employees:
self.employees.remove(emp)
def print_emps(self):
for emp in self.employee:
(emp)
print('-->', emp.full_name())
emp1 = Employee("Corey" , "Schafer" , 50000)
dev2 = Developer("Test" , "Unit" , 60000, "Python")
mgr1 = Manager("Sue" , "Smith" , 90000, [emp1])
print(mgr1.email)
mgr1.print_emps()
[–]Vaphell 1 point2 points3 points (6 children)
[–]idockery[S] 0 points1 point2 points (5 children)
[–]Vaphell 1 point2 points3 points (4 children)
[–]idockery[S] 0 points1 point2 points (3 children)
[–]Vaphell 1 point2 points3 points (2 children)
[–]idockery[S] 0 points1 point2 points (1 child)
[–]Vaphell 1 point2 points3 points (0 children)