This looks like a job for... someone who can explain when self should be used


#1



https://www.codecademy.com/courses/python-intermediate-en-WL8e4/2/4?curriculum_id=4f89dab3d788890003000096#
this looks like a job for....

Oops, try again. Your code raised an exception, have a look at the console window for the error message!
Traceback (most recent call last):
File "python", line 24, in
File "python", line 21, in full_time_wage
TypeError: calculate_wage() takes exactly 2 arguments (3 given)


I expected it to work, But it apparently does not need the self in the arguments in calculate_wage of the full_time_employee method. I am just wondering why it is not needed? because in the calculate wage method in employee it does need the self augment. It works fine if i dont have the self augment.

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

    def calculate_wage(self, hours):
        print hours #debug
        self.hours = hours
        print hours #debug
        print self.hours*20.00 #debug
        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")
milton.full_time_wage(0)


#2

Okay, I think I understand super already calls self. So when you super calculate wage you already defined the first augment. so when you use super its actually

super(sub_class,augment1)parent_method(augment2,augment3,augment4)

so when it calls the parents method its going to actually be

parent_method(augment1,augment2,augment3,augment4).

and
augment1=object_name.


#3

The object that a method is called on is implicitly added as the first argument (otherwise you would be mentioning that value twice, or it would just be a function where you explicitly include the object in the arguments)


#4

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