Possible bug in "This looks like a job for.."


#1

The following call raises the error that is shown following it:

milton = PartTimeEmployee("Miton")
print milton.full_time_wage(20)

TypeError: full_time_wage() takes exactly 2 arguments (3 given)

The same error is returned when three arguments are actually supplied.

print milton.full_time_wage(20, 30)

And when no arguments are passed, i.e.

print milton.full_time_wage()

the error returned is

TypeError: full_time_wage() takes exactly 2 arguments (1 given)

Where am I going wrong with this?

Here's the full thing:

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 self.hours * 12.00
    def full_time_wage(self, hours):
        return super(PartTimeEmployee, self).calculate_wage(self, hours)
        
milton = PartTimeEmployee("Miton")
print milton.full_time_wage()

#2

You're sending self twice when you use super, meaning you send 3 arguments where 2 were expected.

Which also means that the two error messages are not the same, the one you posted doesn't match the function call you described.


#3

Thank you.

I figured out what I was doing wrong - as you pointed out I was sending self twice.
I assumed that since all methods in the class all have self as an argument, I assumed that a super call should also have one.

I corrected the code to:
return super(PartTimeEmployee, self).calculate_wage(hours)
against
return super(PartTimeEmployee, self).calculate_wage(self, hours)


#4

super binds the method to the object (self), so whenever it's called it is associated with that object.

But since you call it immediately, you could alternatively do:

Emplayee.calculate_wage(self, hours)

Here we use an unbound calculate_wage - no associated object, so one has to be specified

Whereas super lets you do things like

# keep the bound method for later
calc_wage = super(PartTimeEmployee, self).calculate_wage
# call the bound method.
print calc_wage(6)

Here the bound method is saved for later in a variable. When it is called at a later time, it is still associated with that same object.


#5

Greetings! There is an optional solution.

class Employee(object):
   
    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)