Python Forum
[Classes] Class Intermediate: Inheritance
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Classes] Class Intermediate: Inheritance
#2
One issue we have with all of this code is that we have to provide 'None' as a location for our investigators. This is a minor point, but let's say we want to change it just to make our code cleaner. To do that we would have to override __init__ for the Investigator class. Again, this is not a big deal, since __init__ just assigns parameters to attributes. But in some of your classes you may have more complicated initialization. Repeating it defeats the purpose of code reuse, introducing potential bugs and problems updating code. The way around that is the super function:

class Investigator(StandardEmployee):
   """
   A field investigator for the ACME Company.
   
   Overridden Methods:
   contact_info
   """
   
   def __init__(self, title, first, last, phone, pay):
      super(Investigator, self).__init__(title, first, last, None, phone, pay)
...
Now we don't have to pass a None location to the Investigator class, it takes care of that itself. The super function finds the method in the base class even if it's overridden in the sub-class. You can also call that method directly, with something like:

      StandardEmployee.__init__(self, title, first, last, None, phone, pay)
But this is generally frowned upon. One problem with it is that if you change the base class of Investigator, you'd have to change it with every call you made to a base class method. Note that in our call to super, we pass the current class, not the base class. The super function figures out what the base class is. This is good for situations we will see later, where it is not as clear where the method we want is.

Also note that we don't pass self as a parameter to the __init__ from super, but we do when calling the one directly from the base class. The super case makes the __init__ method "bound" to the current instance, but the direct call does not.

There is one disadvantage of using different classes for each type of employee. When we had one class we could just call that class and it took care of the differences. Now we have to remember which class goes with which employee. The standard response to this is what is known as a "factory function":

def make_employee(grade, title, first, last, location, phone, pay):
   """
   Create an appropriate employee instance.
   
   Parameters:
   first: The employee's first name. (str)
   grade: The type of employee. (str)
   last: The employee's last name. (str)
   location: Where the employee's desk is. (str)
   pay: The employee's salary or hourly wage. (float)
   phone: The employee's business phone number. (str)
   title: The employee's position. (str)
   """
   if grade == 'Manager':
      employee = Manager(title, first, last, location, phone, pay)
   elif grade == 'Staff':
      employee = StandardEmployee(title, first, last, location, phone, pay)
   elif grade == 'Investigator':
      employee = Investigator(title, first, last, phone, pay)
   else:
      raise ValueError('Unknown grade parameter ({})'.format(grade))
   return employee
Now we can create the instances more consistently, like we did the first time:

# example employees
bob3 = make_employee('Manager', 'Director of Analysis', "Bob", 'Dobbs', '801', 'x0888', 130130)
craig3 = make_employee('Staff', 'Statistician', 'Craig', "O'Brien", '802-18', 'x7666', 25)
ronsarde3 = make_employee('Investigator', 'Inspector', 'Sebastian', 'Ronsarde', None, '123-4567', 18)
We have to pass the grade parameter again, which we had gotten rid of, but it might not be necessary. If our managers always have titles including Director, Executive, or President, and our investigator's titles always include the word Investigator; we could use the title to determine which class to use instead.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures


Messages In This Thread
Class Intermediate: Inheritance - by ichabod801 - Sep-15-2016, 11:16 PM
RE: Class Intermediate: Inheritance - by ichabod801 - Sep-15-2016, 11:18 PM
RE: Class Intermediate: Inheritance - by ichabod801 - Sep-15-2016, 11:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Classes] Class Intermediate: Operator Overloading ichabod801 3 10,639 Sep-15-2016, 11:07 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020