Python Eval执行环境

发布于 2024-08-23 09:06:01 字数 786 浏览 3 评论 0原文

我不明白 eval 或 exec 语句在什么环境中执行。您可以将全局作用域和局部作用域传递给它们,但我不太明白这意味着什么。 python 是否为它们创建一个匿名模块,如果是这种情况,全局范围和局部范围有何不同?

它是否像匿名函数一样运行它?如果是这种情况,全局和局部作用域对我来说会更有意义,尽管您仍然需要调用 global var 来阻止 python 在赋值时创建局部变量吗?

这里有一些代码来展示我实际上想做的事情。

# module level vars
result = ''

allowed_builtins = {"__builtins__":{'int':int, 'str':str, 'range':range, 'dir':dir,
                                    'zip':zip
},
                    "result":result}

在类中,

def _exec(self, answer, function_name, input):
    global result
    exec_string = answer + '\n'
    exec_string += 'global result; result = %s(%s)' % (function_name, input)
    exec exec_string in allowed_builtins, {}

    return result

我希望能够在 eval/exec 的范围内设置我范围内的 var 结果。

I do not understand what environment a eval or exec statement executes in. You can pass both global and local scopes to them but I don't quite understand what this means. Does python create an anonymous module for them, and if that is the case how do the global and local scope differ?

Does it run it like it was an anonymous function? If that was the case the global and local scopes would make more sense to me, although would you still need to call global var to prevent python from making a local variable on assignment?

And here is some code to show what I am actually trying to do.

# module level vars
result = ''

allowed_builtins = {"__builtins__":{'int':int, 'str':str, 'range':range, 'dir':dir,
                                    'zip':zip
},
                    "result":result}

In class

def _exec(self, answer, function_name, input):
    global result
    exec_string = answer + '\n'
    exec_string += 'global result; result = %s(%s)' % (function_name, input)
    exec exec_string in allowed_builtins, {}

    return result

I would like the var result in my scope to be able to be set from within the eval/exec's scope.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阿楠 2024-08-30 09:06:01

“本地”字典是在 exec 或 eval 期间设置所有名称的地方; “全局”用于查找在“本地”中找不到的名称,但名称不会在那里设置,除非您执行包含全局的代码代码>声明。

没有任何模块对象是由 evalexec 本质上创建的,也不是任何函数对象,无论是匿名的还是其他方式(当然,除非您 exec > 语句,例如 def 等)。

编辑:例如,给定OP的代码,并假设_exec是一个独立的函数,因为OP没有给出它可以存在的 ,在末尾添加:

print 'one: %r' % _exec(None, '"foo"', 'range', 7)
print 'two: %r' % allowed_builtins['result']

你会看到这样的输出:

one: ''
two: [0, 1, 2, 3, 4, 5, 6]

当前模块的__dict__中的result当然不受影响(怎么可能 be,因为该字典从未传递给有问题的 exec?!) - allowed_builtins 字典当然是受影响的字典,因为它是字典作为“全局字典”传递,并且正在执行exec的字符串中有一个global语句!

The "local" dictionary is where all names are being set during an exec or eval; the "global" one is used for lookup of names not found in the "local" one, but names aren't set there unless you're execing code that includes a global statement.

No module object is created intrinsically by either eval or exec, nor is any function object, anonymous or otherwise (again, of course: unless you exec statements such as def, etc).

Edit: for example, given the OP's code, and assuming _exec is a free-standing function since the OP's giving no class where it could live, add at the end:

print 'one: %r' % _exec(None, '"foo"', 'range', 7)
print 'two: %r' % allowed_builtins['result']

and you'll see this output:

one: ''
two: [0, 1, 2, 3, 4, 5, 6]

the result in the __dict__ of the current module is of course not affected (how could it conceivably be, since that dict is never passed to the exec in question?!) -- the allowed_builtins dictionary is of course the one affected, since it's the dict passed as the "global dictionary" and there is a global statement in the string being executed!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文