0

Trying to understand inheritance. The PartTimeEmployee class should inherit attributes from the Employee class. But I get errors trying "tap" into the ReturnName method to print out the PartTimeEmployee information.

class Employee(object):
    def __init__(self, employee_name):
        self.employee_name = employee_name

    def ReturnName(self, employee_name):
        self.employee_name = employee_name
        return employee_name

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


class PartTimeEmployee(Employee):
    def calculate_wage(self, hours):
        self.hours = hours
        return hours * 12.00

ft_emp = Employee("Mike")
ft_emp_name = ft_emp.ReturnName("Mike")

pt_emp = PartTimeEmployee("Bill")
#pt_emp_name = pt_emp.ReturnName("Bill")

ft_pay = ft_emp.calculate_wage(40)
#pt_pay = pt_emp_name.calculate_wage(20)

print "Full time employee %s made $%d for the week." %(ft_emp_name, ft_pay)
#print "Part time employee %s made $%d for the week." %(pt_emp_name, pt_pay)

>>>
Full time employee Mike made $800 for the week.
4
  • 4
    What is the error you are getting? Commented May 26, 2017 at 20:02
  • 3
    As as side note, it is a bad idea to have a method called "returnX" and use it to set X. Commented May 26, 2017 at 20:02
  • 2
    Please show the exact error. Commented May 26, 2017 at 20:02
  • With what you have so far, I wouldn't even use inheritance. I would just make wage an attribute Commented May 26, 2017 at 20:03

2 Answers 2

2

The commented line:

#pt_pay = pt_emp_name.calculate_wage(20)

should be:

pt_pay = pt_emp.calculate_wage(20)

Otherwise it is an attempt to call method calculate_wage on a string object.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks piokuc, exactly what I needed.
1

What you really have are two different kinds of employees, full-time and part-time. As such, I might use a design like this:

class Employee(object):
    def __init__(self, name, wage):
        self.name = name
        self.wage = wage

    def calculate_wage(self, hours):
        return hours * self.wage


class FullTimeEmployee(Employee):
    def __init__(self, name):
        super().__init__(name, 20)


class PartTimeEmployee(Employee):
    def __init__(self, name):
        super().__init__(name, 12)

Employee contains everything that is common to either type of employee. The subclasses contain information specific to each type, so far just the hourly wage paid each. Notice there is no ReturnName method. If you want an employee's name, just access the name attribute directly.

ft_emp = FullTimeEmployee("Mike")
ft_emp_name = ft_emp.name

pt_emp = PartTimeEmployee("Bill")
pt_emp_name = pt_emp.name

ft_pay = ft_emp.calculate_wage(40)
pt_pay = pt_emp.calculate_wage(20)

print "Full time employee %s made $%d for the week." %(ft_emp_name, ft_pay)
print "Part time employee %s made $%d for the week." %(pt_emp_name, pt_pay)

2 Comments

Thanks chepner, much more efficient this way.
chepner, I wasn't able to get this to run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.