This Looks Like a Job For


#1


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

Hello,
I'm getting the following error:

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

It says I'm giving three arguments when the function expects two. The two arguments I'm passing are (milton,10). If I delete the 10 and just pass (milton) I get the error that I'm only passing one argument when two are expected. What am I doing wrong? Where is Python getting 1 + 1 = 3?


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)

# Instance
milton = PartTimeEmployee("Milton")
print PartTimeEmployee.full_time_wage(milton,10)


#2

full_time_wage is an instance method. You have to execute it on the instance of the class PartTimeEmployee. And this is why you don't have to pass self, it's already in the context.

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)

# Instance
milton = PartTimeEmployee("Milton")
print milton.full_time_wage(10)

You can also create static methods and class methods, which can be executed without a need of any instance. You can read more about them here -> http://pythoncentral.io/difference-between-staticmethod-and-classmethod-in-python/.


#3

@mattdhall,

They try to bring you closer to inheritance pattern between a [parent]-class and a [child]-class
By using

            class Employee(object) 
                     [parent]
                         ^
                         |
                      [child]
class PartimeEmployee(Employee)

you have created this inheritance pattern
and you should know that the [child]-class can access the properties and method's of the [parent]-class

==========================================================

Now they define 2 same-named Methods

            class Employee(object) 
                calculate_wage(self,hour)
                     [parent]
                         ^
                         |
                      [child]
class PartTimeEmployee(Employee)
                calculate_wage(self,hour)

With this they created the problem
of so-called shadowed Methods
as the Methods have the same name.

If a PartTimeEmployee-class-object now makes a call to the calculate_wage() Method
the calculate_wage() Method of the PartTimeEmployee-class is used
and the calculate_wage() Method of the [parent]-Employee-class will never be used
as the [parent]-Method name is the same, ( therefor =shadowed= )

=============================================

There are 2 possible solutions:

== Solution 1 ( using different Method names ) =======

            class Employee(object) 
                calculate_wageF(self,hour)
                     [parent]
                         ^
                         |
                      [child]
class PartTimeEmployee(Employee)
                calculate_wageP(self,hour)

You could now use the call's

milton.calculate_wageP(10)
#or
milton.calculate_wageF(10)

== Solution 2 ( using the super syntax ) =============

            class Employee(object) 
               def calculate_wage(self,hour)    <------<<------+
                     [parent]                                  |
                         ^                                     |
                         |                                     |
                      [child]                                  ^
class PartTimeEmployee(Employee)                               |
               def calculate_wage(self,hour)                   |
                                                               ^
               def full_time_wage(self,hours):                 |
        return super(PartTimeEmployee, self).calculate_wage(self,hours)

What is wrong with the inherinted class?
Class syntax confusion
#4

Thank you so much! Works perfectly. :+1:


#5

Thank you so much, this is an amazing explaination! I'm going to print this out as a reference for the rest of the lessons on classes.