0

I'm trying to get a RPN calculator by reusing the class CalculatorEngine as follows. But when I run it, it shows an Attribute Error: 'RPNCalculator' object has no attribute 'dataStack'. How do I solve this? (I didn't include the Stack class as there'd be too much code.)

     class CalculatorEngine(object):
        def __init__(self):
           self.dataStack = Stack()

        def pushOperand(self, value):
           self.dataStack.push(value)

        def currentOperand(self):
           return self.dataStack.top()

        def performBinary(self, fun):
           right = self.dataStack.pop()
           left = self.dataStack.top()
           self.dataStack.push(fun(left, right))

        def doAddition(self):
           self.performBinary(lambda x, y: x + y)

        def doSubtraction(self):
           self.performBinary(lambda x, y: x - y)

        def doMultiplication(self):
           self.performBinary(lambda x, y: x * y)

        def doDivision(self):
           try:
              self.performBinary(lambda x, y: x / y)
           except ZeroDivisionError:
              print("divide by 0!")
              exit(1)

        def doTextOp(self, op):
           if (op == '+'):
              self.doAddition()
           elif (op == '-'):
              self.doSubtraction()
           elif (op == '*'):
              self.doMultiplication()
           elif (op == '/'): self.doDivision()


     class RPNCalculator(CalculatorEngine):
        def __init__(self):
           super(CalculatorEngine, self).__init__()

        def eval(self, line):
           op = line.split(" ")
           try:
              for item in op:
                 if item in '+-*/':
                    self.doTextOp(item)
                 elif item in '%':
                    self.performBinary(lambda x, y: x % y)
                 else:
                    self.pushOperand(int(item))
              return self.currentOperand()
           except ZeroDivisionError:
              print 'divide by 0!'
Tiffy
  • 1
  • 2
  • Where is the rest of the code (instance creation and manipulation, and of course the line that generates the exception)? Also, fix the indentation. – CristiFati Oct 11 '17 at 15:49
  • https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ –  Oct 11 '17 at 17:19

1 Answers1

0

class X(object):
  def __init__(self, name):
    self._name = name

  def doit(self, bar):
    print("Hello")

class Y(X):
  def __init__(self):
    # super(X, self).__init__()
    X.__init__(self, "DDXX")


i = X("Yuze")
j = Y()

Or you can use this snippet to fix it.

Yuze Ma
  • 357
  • 1
  • 9
  • Yes I did. I didn't add the codes cause it wouldn't allow me to post :) – Tiffy Oct 11 '17 at 15:13
  • I see. I think it is because of the usage of the superclass. I recommend use the method in this link https://stackoverflow.com/questions/576169/understanding-python-super-with-init-methods – Yuze Ma Oct 11 '17 at 18:32