1

I'm learning about inheritance and polymorphism at the moment and this code is supposed to test those concepts. When I attempt to instantiate the subclass object the init constructor doesn't recognize the method. Here's the code.

class Employee:

    # constructor
    def __init__(self, name, empnum):
        self.__empName = name
        self.__empNum = empnum
        #setName(name)
        #setNumber(empnum)

    # setters
    def setName(self, name):
        self.__empName = name

    def setNumber(self, number):
        if len(str(number)) == 5:
            self.__empNum = number
        else:
            print('Too many or too little numbers entered. Please try again')
            setNumber()

    # getters
    def getName(self):
        return self.__empName

    def getNumber(self):
        return self.__empNum


class ProductionWorker(Employee):

    # Overwritten constructor
    def __init__(self, name, empnum, shiftnum, rate):
        self.setShiftNum(shiftnum)
        self.setPayRate(rate)
        # Calls Superclass Method
        Employee.__innit__(self, name, empnum)

    # Setters
    def setShiftNum(self, num):
        if num == 1 | 2:
            self.__shiftNum = num
        else:
            print('Shift number needs to be a 1 or 2. Please try again')
            setShiftNum(self, num)

    def setPayRate(self, rate):
        self.__payRate = '$' + str(format(rate, ',.2f'))

    # Getters
    def getShiftNum(self):
        return self.__shiftNum

    def getPayRate(self):
        return self.__payRate

def main():
    gruntling = Employee('Farlo', 53400)
    print('Your grunts name is' + gruntling.getName())
    print('this program does things. We swears.')

    grunty = ProductionWorker('Farlo', 45300, 2, 4.25)
    print('Lil grunty\'s name is ' + grunty.getName())

main()

The error I get is:

Traceback (most recent call last):
  File "C:/Users/sessh/Dropbox/Spring >2016/Python/Assignments/S13/company_employee.py", line 69, in <module>
    main()
  File "C:/Users/sessh/Dropbox/Spring >2016/Python/Assignments/S13/company_employee.py", line 66, in main
    grunty = ProductionWorker('Farlo', 45300, 2, 4.25)
  File "C:/Users/sessh/Dropbox/Spring >2016/Python/Assignments/S13/company_employee.py", line 38, in __init__
    self.setShiftNum(shiftnum)
  File "C:/Users/sessh/Dropbox/Spring >2016/Python/Assignments/S13/company_employee.py", line 49, in setShiftNum
    setShiftNum(self, num)
NameError: name 'setShiftNum' is not defined

ed

I browsed around and the closest example I could find was Calling a class function inside of __init__

The best fix I could find was to add self to the function within init but then I got some weird error about there being too many arguments. It's madness I say!

Thank you for your time.

EDIT. Alright, I read some of the comments and altered the logic of the function giving me issues. Also, I didn't realize that the error was completely due to the compiler not seeing the function because it was bad. That's an interesting bit of knowledge:

# Overwritten constructor
def __init__(self, name, empnum, shiftnum, rate):
    #self.__shiftNum = shiftnum
    self.__payRate = rate
    self.setShiftNum(shiftnum)
    # Calls Superclass Method
    Employee.__init__(self, name, empnum)

# Setters
def setShiftNum(self, num):
    shiftnum = num
    if shiftnum == 1 or shiftnum == 2:
        self.__shiftNum = shiftnum
    else:
        while True:
            shiftnum = input('Shift Number MUST BE a 1 or a 2. Enter it now: ')
            if shiftnum == 1 or shiftnum == 2:
                self.__shiftNum = numshiftnum
                break
            else:
                continue

So as you saw with the recursive function previously. I love infinite loops. So I created another one, without any sort of intent I assure you. This code actually works if a 1 or 2 is passed to it but if it's not you'll get trapped in a never ending cycle of a passive aggressive program demanding your subservience to typing in 1's and 2's

At this point it seems I'm just stuck on the logic of the situation, so I think I'm close to the solution. I have a feeling the logic has pretty poor Algorithm Complexity but, at this point, I just want it to work.

Community
  • 1
  • 1
Rualani
  • 13
  • 5
  • Try `self.setShiftNum(num)`. – Code-Apprentice Apr 18 '17 at 02:09
  • Note that you also have an infinite recursion. A recursive setter is poor design. Instead it should throw an exception or otherwise indicate that an incorrect parameter. The caller then is responsible for handling the error. – Code-Apprentice Apr 18 '17 at 02:33
  • You might find some ideas here - http://stackoverflow.com/a/23294659/2823755 – wwii Apr 18 '17 at 02:57
  • Where did you define the method? – Ignacio Vazquez-Abrams Apr 18 '17 at 03:03
  • Okay, removed the recursive design and took some inspiration from wwii's link in setting up a proper validation loop. I think the error was occuring just because the compiler was rejecting the method and not seeing it. I also saw the error pop up when troubleshooting some other recursive variants. – Rualani Apr 18 '17 at 18:19

1 Answers1

0
def setShiftNum(self, num):
    if num == 1 or num == 2:
        self.__shiftNum = num
    else:
        print('Shift number needs to be a 1 or 2. Please try again')
        self.setShiftNum(num)

Please be aware of that your logic makes it run into an infinite loop.
To prevent that consider replacing the recursion block to something like self.setShiftNum(num = 1) or change your logic.

Devendra Lattu
  • 2,732
  • 2
  • 18
  • 27
  • Logic changed. Thanks for the catch. – Rualani Apr 18 '17 at 18:19
  • Alright. So has this issue been resolved even though I updated the problem. The problem changed slightly into a question about loop logic so should I close this one? – Rualani Apr 18 '17 at 20:03
  • I am not sure about that as I haven't gone through the updated one. Maybe some people might find my solution helpful, so don't close if you are not sure. – Devendra Lattu Apr 18 '17 at 20:56
  • Ah, well. I upvoted but my rep is lower than 15 so public score not changed. – Rualani Apr 18 '17 at 22:02