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!'