Search code examples
pythonmethodssuperpython-class

wrong result printed to the console


I have the following code, which should print 3500.0 to the console:

class Employee:
    
    """ Creates an instance of Employee """
    
    def __init__(self, name, annual_salary):
        self.name = name
        self.annual_salary = annual_salary
        
    def calculate_monthly_salary(self):
        return annual_salary / 0.12
        

class CustomerServiceEmployee(Employee):
    
    """ Creates an instance of CustomerServiceEmployee """
    
    def __init__(self, name, annual_salary, department):
        super().__init__(name, annual_salary)
        self.department = department

cs_manager = CustomerServiceEmployee("Kelly Johnson", 42000, "Customer Service")
kellys_monthly_salary = Employee.calculate_monthly_salary
print(kellys_monthly_salary)

but instead it prints:

<function Employee.calculate_monthly_salary at 0x7fdfb1db31f0>

I am led to believe my issue is in the method calculate_monthly_salary but I can't for the life of me sort it out. Could anybody shed some light please?


Solution

  • First of all you have created an object for CustomerServiceEmployee already and stored that in cs_manager. So why not use it?

    kellys_monthly_salary = cs_manager.calculate_monthly_salary()
    

    also line 10 should have self.

    self.annual_salary 
    

    full code

    class Employee:
        
        """ Creates an instance of Employee """
        
        def __init__(self, name, annual_salary):
            self.name = name
            self.annual_salary = annual_salary
            
        def calculate_monthly_salary(self):
            return self.annual_salary / 0.12
            
    
    class CustomerServiceEmployee(Employee):
        
        """ Creates an instance of CustomerServiceEmployee """
        
        def __init__(self, name, annual_salary, department):
            super().__init__(name, annual_salary)
            self.department = department
    
    cs_manager = CustomerServiceEmployee("Kelly Johnson", 42000, "Customer Service")
    kellys_monthly_salary = cs_manager.calculate_monthly_salary()
    print(kellys_monthly_salary)