我正在尝试通过重用类CalculatorEngine来获得一个RPN计算器,如下所示。但是当我运行它时,它显示了一个属性错误:'RPNCalculator‘对象没有'dataStack’属性。我该如何解决这个问题?(我没有包含Stack类,因为这样会有太多代码。)
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!'发布于 2017-10-11 23:10:49
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()
或者,您可以使用此代码片段来修复它。
https://stackoverflow.com/questions/46691704
复制相似问题