你如何只在一个对象上下文中做一个python'eval'?

Vin*_*sal 8 python eval abstract-syntax-tree

是否有可能做类似的事情

c = MyObj()
c.eval("func1(42)+func2(24)")
Run Code Online (Sandbox Code Playgroud)

在Python ..我有func1()和func2()在对象'c'的范围内进行评估(如果它们是该类定义中的成员函数)?我不能做一个简单的解析,因为对于我的应用程序,eval字符串可能变得任意复杂.我想用ast模块做一些魔法可能会有所作为,但是由于文学史上的d ,,我不知道在哪里看:

import ast

class MyTransformer(ast.NodeTransformer):
    def visit_Name(self, node):
        # do a generic_visit so that child nodes are processed
        ast.NodeVisitor.generic_visit(self, node)
        return ast.copy_location(
            # do something magical with names that are functions, so that they become 
            # method calls to a Formula object
            newnode,
            node
        )

class Formula(object):

    def LEFT(self, s, n):
        return s[:n]

    def RIGHT(self, s, n):
        return s[0-n:]

    def CONCAT(self, *args, **kwargs):
        return ''.join([arg for arg in args])

def main():

    evalString = "CONCAT(LEFT('Hello', 2), RIGHT('World', 3))"

    # we want to emulate something like Formula().eval(evalString)
    node = ast.parse(evalString, mode='eval')
    MyTransformer().visit(node)

    ast.fix_missing_locations(node)
    print eval(compile(node, '<string>', mode='eval'))    
Run Code Online (Sandbox Code Playgroud)

aba*_*ert 9

你几乎肯定不想这样做,但你可以.

对于上下文eval是要评估在你的代码中的全局和当地人词典.最常见的情况是可能eval(expr, globals(), mycontext)eval(expr, mycontext),取代默认的本地和全球范围内,分别留下其他孤单.用对象的字典替换本地上下文类似于运行该对象的"内部"(一种方法) - 尽管记住"成为一个成员函数"并没有像你想象的那样做得那么好有一个self叫其他成员函数...

无论如何,这是一个简单的例子:

>>> class Foo(object):
...     def __init__(self):
...         self.bar = 3
>>> foo = Foo()
>>> eval('a', globals(), foo.__dict__)
3
Run Code Online (Sandbox Code Playgroud)

请记住,这__dict__可能不是您想要的.例如:

>>> class Foo(object):
...     @staticmethod
...     def bar():
...         return 3
>>> foo = Foo()
>>> eval('bar()', globals(), foo.__dict__)
NameError: name 'bar' is not defined
>>> eval('bar()', globals(), {k: getattr(foo, k) for k in dir(foo)}
3
Run Code Online (Sandbox Code Playgroud)

为了使你的工作按照你想要的方式工作,你必须确切地知道如何定义你想要的,用Python术语 - 这需要了解对象如何在封面下工作(MRO,可能是描述符等).

如果你确实需要eval,并且你真的需要提供任意上下文,那么你可能更好地明确地构建这些上下文(作为字典),而不是试图强迫对象进入该角色:

>>> foo = {
...     'bar': lambda: 3
... }
>>> eval('bar()', globals(), foo)
Run Code Online (Sandbox Code Playgroud)

这种用法更接近你试图在Python中模拟的Javascript风格.

当然,与JS不同,Python不允许您在表达式中放置多行定义,因此对于复杂的情况,您必须这样做:

>>> def bar():
...     return 3
>>> foo = {
...     'bar': bar
... }
>>> eval('bar()', globals(), foo)
Run Code Online (Sandbox Code Playgroud)

但可以说这几乎总是更具可读性(这基本上是Python背后不允许在表达式中使用多行定义的论点).