我正在尝试在numexpr表达式中使用对象属性。最明显的方法是:import numpy as npimport numexpr as neclass MyClass: def __init__(self): self.a = np.zeros(10)o = MyClass()o.ab = ne.evaluate("o.a+1")导致以下错误---------------------------------------------------------------------------AttributeError Traceback (most recent call last)<ipython-input-22-dc90c81859f1> in <module>() 10 o.a 11 ---> 12 b = ne.evaluate("o.a+1")~/.local/lib/python3.5/site-packages/numexpr/necompiler.py in evaluate(ex, local_dict, global_dict, out, order, casting, **kwargs) 799 expr_key = (ex, tuple(sorted(context.items()))) 800 if expr_key not in _names_cache:--> 801 _names_cache[expr_key] = getExprNames(ex, context) 802 names, ex_uses_vml = _names_cache[expr_key] 803 arguments = getArguments(names, local_dict, global_dict)~/.local/lib/python3.5/site-packages/numexpr/necompiler.py in getExprNames(text, context) 706 707 def getExprNames(text, context):--> 708 ex = stringToExpression(text, {}, context) 709 ast = expressionToAST(ex) 710 input_order = getInputOrder(ast, None)~/.local/lib/python3.5/site-packages/numexpr/necompiler.py in stringToExpression(s, types, context) 296 names.update(expressions.functions) 297 # now build the expression--> 298 ex = eval(c, names) 299 if expressions.isConstant(ex): 300 ex = expressions.ConstantNode(ex, expressions.getKind(ex))<expr> in <module>()AttributeError: 'VariableNode' object has no attribute 'a'咨询另一个问题,我能够通过使用numexpr's得到一个不太令人满意的解决方案global_dict:import numpy as npimport numexpr as neclass MyClass: def __init__(self): self.a = np.zeros(10)o = MyClass()o.ab = ne.evaluate("a+1", global_dict={'a':o.a})一旦MyClass有十几个属性并且有几个这样的调用,这将变得非常混乱ne.evaluate。有没有一种简单,干净的方法来做到这一点?
添加回答
举报
0/150
提交
取消