7

I'm having trouble with my code. I'm trying to create a subclass which inherits the parent class's attributes and methods but it doesn't work. Here's what I have so far:

class Employee(object): 
  def __init__(self, emp, name, seat):
    self.emp = emp
    self.name = name
    self.seat = seat

Something is wrong with the block of code below - the subclass.

Do I have to create the __init__ again? And how do I create a new attribute for the subclass. From reading questions, it sounds like __init__ in the subclass will override the parent class - is that true if I call it to define another attribute?

class Manager(Employee): 
  def __init__(self, reports):
    self.reports = reports
    reports = [] 
    reports.append(self.name) #getting an error that name isn't an attribute. Why? 

  def totalreports(self):
    return reports

I want the names from the Employee class to be in the reports list.

For example, if I have:

emp_1 = Employee('345', 'Big Bird', '22 A')
emp_2 = Employee('234', 'Bert Ernie', '21 B')

mgr_3 = Manager('212', 'Count Dracula', '10 C')

print mgr_3.totalreports()

I want reports = ['Big Bird', 'Bert Ernie'] but it doesn't work

user1589244
  • 435
  • 3
  • 7
  • 12
  • possible duplicate of [Error: "x instance has no attribute y" when trying to inherit from class](http://stackoverflow.com/questions/6143693/error-x-instance-has-no-attribute-y-when-trying-to-inherit-from-class) – Ignacio Vazquez-Abrams Aug 22 '12 at 05:01
  • `I want reports = ['big', 'bird'] but it doesn't work` -- Do you mean `reports = ['Big Bird', 'Bert Ernie']`? If so, that's not how inheritance works. The Manager class doesn't know about any *instances* of Employee that you have previously created. Or are you getting a different error? – Adam Lear Aug 22 '12 at 05:04

2 Answers2

9

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.

Blender
  • 289,723
  • 53
  • 439
  • 496
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? 
Paul McNett
  • 847
  • 6
  • 9