syntactic sugar
syntactic sugar

Reputation: 11

Python error: object has no attribute

I'm new to coding -- taking a Python college course. I know this will be obvious to many of you, but I can not figure out why I continue to get this error attribute error:

prodworker = employee.ProductionWorker(shift_num, pay_rate)
AttributeError: 'Employee' object has no attribute 'ProductionWorker'

Any help is greatly appreciated :)

class Employee: #writing new class called Employee:

    def __init__(self, name, number): #accepts arguments for employee name and 
        self.__name = name              #employee number 
        self.__number = number

    def set_name(self, name): #mutator methods to set name and number
    self.__name = name



 def set_number(self, number):
        self.__number = number


#accessor methods returns name and number
    def get_name(self):
        return self.__name

    def get_number(self):
        return self.__number



class ProductionWorker(Employee): #write subclass



    def __init__(self, shift_num, pay_rate):
        Employee.__init__(self, 'ProductionWorker')
        self.__shift_num = shift_num
        self.__pay_rate = pay_rate

    def set_shift_num(self, shift_num):
        self.__shift_num = shift_num

    def set_pay_rate(self, pay_rate):
        self.__pay_rate = pay_rate     

    def get_shift_num(self):
        return self.__shift_num

    def get_pay_rate(self):
        return self.__pay_rate



#This program creates an instance of Employee Class
#and an instance of Production Worker Class:

again = 'Y'
while again.upper() == 'Y':
    print('Enter the following data for the employee: \n')
    name = input('What is the employee name?: ')
    number = input('What is the employee number? ')


    shift_num = input('What is the employee shift number? 1 = Day, 2 = Night :') 
    while shift_num != '1' and shift_num != '2':
        shift_num = input('Invalid entry! Enter 1 for Day shift or 2 for Night shift!')
    else:
        if shift_num == '1':
            shift_num = 'Day'
        if shift_num == '2':
            shift_num = 'Night'

    pay_rate = float(input('What is the employee pay rate? '))

    print()
    print('This is an instance of the Employee class:') 
    employee = Employee(name, number)   
    print('EMPLOYEE: \t\t'+ employee.get_name())
    print('EMPLOYEE NUMBER: \t' + employee.get_number())
    print()

    print('This is an instance of the Production Worker class: ')
    prodworker = employee.ProductionWorker(shift_num, pay_rate)

    print('SHIFT: \t\t\t' + ProductionWorker.get_shift_num())
    print('PAY RATE: \t\t$' + format(ProductionWorker.get_pay_rate(), ',.2f'), sep='')
    print('--------------------------------------------')

    again = input('Enter Y to add another: ')
    if again.upper() != 'Y':
        print('Program End')

Upvotes: 1

Views: 1037

Answers (1)

Blckknght
Blckknght

Reputation: 104692

The ProductionWorker class is a subclass of the Employee class, but that doesn't mean you can call it through an instance of Employee. It's still a top-level class that you should call directly. Try replacing employee.ProductionWorker(...) with just ProductionWorker(...).

You'll get past the current error, but you may have new ones. For instance, I think the current attempt to call Employee.__init__ from ProductionWorker.__init__ will fail because it doesn't pass the right number of arguments. You may also have logic issues, if you expected employee.ProductionWorker to create a ProductionWorker instance that was related in some way to the employee object.

I'd also discourage you from using __double_leading_underscore names for your attributes. That invokes Python's name mangling system, which is mostly intended to help prevent accidental reuse of the same name from different classes in a large or unpredictable inheritance hierarchy. If you just want your attributes to be "private", use a single underscore. That doesn't protect them from being accessed by outside code, but it serves as documentation that they're not part of the object's public API. One of Python's design philosophies is that it's programmers are responsible for their own behavior (often described with the phrase "We're all consenting adults"). If a programmer wants to access an object's private data they can do so (which can be very useful for debugging). But if they break stuff, they have no one to blame but themselves.

Upvotes: 1

Related Questions