This Looks Like a Job For... : arguments taken by calculate_wage


#1

https://www.codecademy.com/en/courses/python-intermediate-en-WL8e4/2/4?curriculum_id=4f89dab3d788890003000096#

Hi everyone! I hope you've enjoyed your week so far. I've solved the issue with my code, but I don't quite know why this works. Below is the code I used to have:

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(self, hours)
        
milton = PartTimeEmployee('milton')
print milton.full_time_wage(20)

I'd get the error:

Traceback (most recent call last):
  File "python", line 19, in <module>
  File "python", line 16, in full_time_wage
TypeError: calculate_wage() takes exactly 2 arguments (3 given)

However, I then used the code below, which worked perfectly:

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(20)

as you can see, I've merely deleted from the full_time_wage() method one of the arguments from calculate_wage, the self argument. However, I noticed that the hint told us the following:

"Your code should look something like this:

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

Where method is the method you want (calculate_wage) and args are the arguments that method takes."

The method we want (calculate_wage) takes two arguments, self and hours, as shown below:

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

Since this is the case, why does using this code not work (as shown below)?

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

Whether or not this worked depended on whether or not I had the self argument after calculate_wage. Again, since the self is an argument taken by calculate_wage, why is it necessary to expunge that argument from calculate_wage when defining the full_time_wage method recursively, using calculate_wage? I know there's a similar post about this, but I want to know exactly why I can't put the self argument into calculate_wage (if self is even an argument at all). Thank you very much for your time!


#2

Did you try passing in only hours?


#3

Thanks for the reply! I did pass only hours, and it did work. However, I'd like to know why the self argument needs to be removed from calculate_wage when defining full_time_wage recursively with it in order for full_time_wage to work properly. Doesn't calculate_wage take 2 arguments, and thus should need to be

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

instead of

super(PartTimeEmployee, self).calculate_wage(hours)

(which I do know is the correct version)?


#4

self is passed to the super.


#5

Thanks for the reply. This makes sense now!


#6

A post was split to a new topic: Just want to ask some questions


#7