Overiding classes and using super()


#1

I started the course for python and its a good course. Tho i came across something i do not realy understand. At the lesson we are introduced with the super() method to get the old functionality after overriding a function.

Here the code where this is used:

class Employee(object):
  """Models real-life employees!"""
  def __init__(self, employee_name):
    self.employee_name = employee_name

  def calculate_wage(self, hours):
    self.hours = hours
    return hours * 20.00

# Add your code below!
class PartTimeEmployee(Employee):
  def calculate_wage(self, hours):
    self.hours = hours
    return hours*12.00
  def full_time_wage(self, hours):
    return super(PartTimeEmployee, self).calculate_wage(hours)

what is strange to me is why PartTimeEmployee is given in the method.

super(PartTimeEmployee, self).calculate_wage(hours)

I tried playing around with it and giving other classes or other children but that seems to be throwing an error.

Why do i need to give PartTimeEmployee in the super() method ?

Is it possible to give other classes to the super() method ?

I have looked for documentation on this subject but i cant find any that addresses this very topic.


#2

although super() is often used in methods, its just a built-in python function:

class Employee(object):
  """Models real-life employees!"""
  def __init__(self, employee_name):
    self.employee_name = employee_name

  def calculate_wage(self, hours):
    self.hours = hours
    return hours * 20.00

# Add your code below!
class PartTimeEmployee(Employee):
  def calculate_wage(self, hours):
    self.hours = hours
    return hours*12.00
  def full_time_wage(self, hours):
    return super(PartTimeEmployee, self).calculate_wage(hours)

class Example(PartTimeEmployee):
    pass

e = Example("name")
print super(PartTimeEmployee, e).calculate_wage(5)
print super(Example, e).calculate_wage(5)

but the first parameter of super is type:

https://docs.python.org/2/library/functions.html#super

which needs to be a class, so in your code, there isn’t much else you can use.

of course we can change this:

class Employee(object):
  """Models real-life employees!"""
  def __init__(self, employee_name):
    self.employee_name = employee_name

  def calculate_wage(self, hours):
    self.hours = hours
    return hours * 20.00

# Add your code below!
class PartTimeEmployee(Employee):
  def calculate_wage(self, hours):
    self.hours = hours
    return hours*12.00
  def full_time_wage(self, hours):
    return super(PartTimeEmployee, self).calculate_wage(hours)

class Example(PartTimeEmployee):
    def full_time_wage(self, hours):
        return super(PartTimeEmployee, self).calculate_wage(hours)

e = Example("name")
print e.full_time_wage(5)

full_time_wage of Example class ends up calling full_time_wage of Employee class. Its skipping it direct parent. Because we provided PartTimeEmployee as type.


#3

Ah i get it. I tried something similar with Employee. Doesn’t work for now obvious reasons. Thanks.

class Employee(object):
  """Models real-life employees!"""
  def __init__(self, employee_name):
    self.employee_name = employee_name

  def calculate_wage(self, hours):
    self.hours = hours
    return hours * 20.00

# Add your code below!
class PartTimeEmployee(Employee):
  def calculate_wage(self, hours):
    self.hours = hours
    return hours*12.00
  def full_time_wage(self, hours):
    return super(Employee, self).calculate_wage(hours)

#4

but that won’t work because Employee has no parent class with full_time_wage method. Which is why i added an additional inheritance layer, then it works


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.