Introduction to classes 14/18


#1



https://www.codecademy.com/fr/courses/python-intermediate-fr-FR-je5q0/2/4?curriculum_id=5370f030fed2a866a2000001#


I don't see where's my mistake !


class Employe(object):
    """Modèles employés réels!"""
    def __init__(self, employe_nom):
        self.employe_nom = employe_nom

    def calculer_salaire(self, heures):
        self.heures = heures
        return heures * 20.00

# Ajoutez votre code en dessous!
class EmployeTempsPartiel(Employe):
    def calculer_salaire(self, heures):
        self.heures = heures
        return heures * 12.00

    def salaire_temps_complet(self, heures):
        self.heures = heures
        return super(EmployeTempsPartiel, self).salaire_temps_complet(heures)
milton = EmployeTempsPartiel(Employe)
print milton.salaire_temps_complet(10)

Oups, merci de réessayer. Votre code soulève une exception, jetez un oeil à la console pour le message d'erreur! 

In the console :

raceback (most recent call last):
  File "python", line 21, in <module>
  File "python", line 18, in salaire_temps_complet
AttributeError: 'super' object has no attribute 'salaire_temps_complet'


#2

Resolved.

My mistake was in the second part of the "super".

return super(EmployeTempsPartiel, self).salaire_temps_complet(heures)
Like that it is better.
return super(EmployeTempsPartiel, self).calculer_salaire(heures)


#3

.... hi, guy thanks!


#4

You are welcome.
De nada.
De rien.
:sunglasses:


#5

Please help me. I do not understand Spanish.

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

#6

How many arguments are there in:

(10, 20)

They said 3!


#7

Hi, @2guygaming ,

The following statement attempts to call the calculate_wage method of the superclass of the PartTimeEmployee class, which is the Employee class ...

return super(PartTimeEmployee, self).calculate_wage(10, 20)

That is what it should do. However, this part of the statement must pass it the correct number of arguments ...

calculate_wage(10, 20)

This is the header of the method that the statement is attempting to call ...

def calculate_wage(self, hours):

That header contains the two parameters, self and hours. But when we call a method of a class, we use dot notation to do so. Using dot notation, the following example of two statements instantiates an Employee, assigns the instance to jane, and finally calls the calculate_wage method on that instance for 40 hours of work, printing the result ...

jane = Employee("Jane Doe")
print jane.calculate_wage(40)

Note that we have only one argument in the parentheses, namely 40, even though there are two parameters specified in the method header.

In reality, two arguments were given. The 40 gets passed to the hours parameter that we have in the method header. jane, which is the object reference on which we called the method, gets passed to the self parameter, serving as the first argument. That's how using dot notation to call a method works.

What you should do instead of this, which constitutes using three arguments ...

return super(PartTimeEmployee, self).calculate_wage(10, 20)

... is this ...

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

In the above, the object on which we are calling the calculate_wage wage method is ...

super(PartTimeEmployee, self)

... which is what comes before the dot. That will be passed to the self parameter that we have in Employee's calculate_wage method header. The hours argument that we have in the parentheses gets its value from an original call to full_time_wage, and gets passed to the hours parameter of Employee's calculate_wage method.

So, when you had 10 and 20 in parentheses, there were really three arguments, with this becoming the first one of the three ...

super(PartTimeEmployee, self)

As your final statement, to call the full_time_wage method, you should supply one argument within the parentheses, such as 10, for ten hours ...

print milton.full_time_wage(10)

In the above, milton takes on the role of an argument that matches the self parameter.


#8

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(10)
milton = PartTimeEmployee('Milton')
print milton.full_time_wage

thanks for that


#9

@2guygaming ,

It would still be best to change this method reference ...

print milton.full_time_wage

... to this method call ...

print milton.full_time_wage(10)

... for more informative output.


#10

this answered helped me as well, thanks! :slight_smile:


#11

You are welcome.
happy coding.


#12

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