问题描述
我不断收到该错误:“ AttributeError:'str'对象没有属性'eval'”,有人可以告诉我如何解决它及其含义吗?这是我的代码顺便说一句:
from rply.token import BaseBox
class Loop(BaseBox):
def __init__(self,number,statementlist):
self.number = number
self.statementlist = statementlist
def eval(self):
for i in range(self.number):
self.statementlist.eval()
@staticmethod
def gettokentype():
return 'statement'
class While(BaseBox):
def __init__(self,condition,statementlist):
self.condition = condition
self.statementlist = statementlist
def eval(self):
while bool(self.condition.eval()):
self.statementlist.eval()
@staticmethod
def gettokentype():
return 'statement'
整个错误:
Traceback (most recent call last):
File "main.py",line 102,in <module>
parser.parse(tokens)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/rply/parser.py",line 49,in parse
current_state = self._reduce_production(
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/rply/parser.py",line 80,in _reduce_production
value = p.func(targ)
File "/home/runner/RealMotionlessLivedistro/Core/parser.py",line 44,in program
return p[0].eval()
File "/home/runner/RealMotionlessLivedistro/Core/AST/Statements.py",line 27,in eval
i.eval()
File "/home/runner/RealMotionlessLivedistro/Core/AST/Loops.py",line 25,in eval
self.statementlist.eval()
File "/home/runner/RealMotionlessLivedistro/Core/AST/Statements.py",in eval
i.eval()
File "/home/runner/RealMotionlessLivedistro/Core/AST/Conditions.py",line 11,in eval
i.eval()
File "/home/runner/RealMotionlessLivedistro/Core/AST/Statements.py",line 9,in eval
self.exp.eval()
File "/home/runner/RealMotionlessLivedistro/Core/AST/Functions.py",line 55,in eval
self.value = self.value.eval()
AttributeError: 'str' object has no attribute 'eval'
我们非常感谢您的帮助,请xD
解决方法
如果您正在查看python内置函数eval()
,则其用法有所不同。
>>> x = 1
>>> eval('x+1')
2