Subclass not inheriting parent class
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Magical Minnie Puzzles
--
Chapters
00:00 Subclass Not Inheriting Parent Class
01:01 Accepted Answer Score 10
02:00 Answer 2 Score 1
02:16 Thank you
--
Full question
https://stackoverflow.com/questions/1206...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #inheritance #attributes #parentchild
#avk47
ACCEPTED ANSWER
Score 10
You never called the parent class's __init__ function, which is where those attributes are defined:
class Manager(Employee): 
  def __init__(self, reports):
    super(Manager, self).__init__()
    self.reports = reports
To do this, you'd have to modify the Employee class's __init__ function and give the parameters default values:
class Employee(object): 
  def __init__(self, emp=None, name=None, seat=None):
    self.emp = emp
    self.name = name
    self.seat = seat
Also, this code will not work at all:
  def totalreports(self):
    return reports
reports's scope is only within the __init__ function, so it will be undefined. You'd have to use self.reports instead of reports.
As for your final question, your structure won't really allow you to do this nicely. I would create a third class to handle employees and managers:
class Business(object):
  def __init__(self, name):
    self.name = name
    self.employees = []
    self.managers = []
  def employee_names(self);
    return [employee.name for employee in self.employees]
You'd have to add employees to the business by appending them to the appropriate list objects.
ANSWER 2
Score 1
You need to run the superclass's init() in the appropriate place, plus capture the (unknown to the subclass) arguments and pass them up:
class Manager(Employee): 
  def __init__(self, reports, *args, **kwargs):
    self.reports = reports
    reports = [] 
    super(Manager, self).__init__(*args, **kwargs)
    reports.append(self.name) #getting an error that name isn't an attribute. Why?