Number of arguments (Classes - 14: This Looks Like a Job For...)


#1
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)

milton = PartTimeEmployee('Milton')
print milton.full_time_wage(10)

As in the example (answer) code above, in method full_time_wage, why is the method here takes only one argument?

Other than the code below, which is my code (TAT)…

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

I now know the right answer, but I just don’t understand it…

Thanks for helping me out here.


#2

Do you mean this line?


#3

No, it’s this line actually:

return super(PartTimeEmployee, self).calculate_wage(hours)

We have overriden a method in super-class, but we still wanna use it. But when we call .calculate_wage(hours) from the super-class, it takes only hours as its argument other than self and hours.

Is it because we have to include self only when we are defining a new method?

Thanks for your reply


#4

The object itself is added before the method is called, the method is bound to the object.
For example:

stuff = []
my_bound_method = stuff.append  # obtain append method bound to stuff
my_bound_method(5)  # since this is append bound to stuff, calling it will append 5 to stuff
my_bound_method(5)
my_bound_method(5)
print stuff  # [5, 5, 5]

If you print the method itself, it’ll tell you which object it is bound to:

print my_bound_method # <built-in method append of list object at 0x7f091950e680>
print hex(id(stuff))  # 0x7f091950e680

As you see, the id matches that of stuff


#5

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