Python Eval执行环境
我不明白 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“本地”字典是在 exec 或 eval 期间设置所有名称的地方; “全局”用于查找在“本地”中找不到的名称,但名称不会在那里设置,除非您
执行
包含全局
的代码代码>声明。没有任何模块对象是由
eval
或exec
本质上创建的,也不是任何函数对象,无论是匿名的还是其他方式(当然,除非您exec
> 语句,例如def
等)。编辑:例如,给定OP的代码,并假设
_exec
是一个独立的函数,因为OP没有给出它可以存在的类
,在末尾添加:你会看到这样的输出:
当前模块的
__dict__
中的result
当然不受影响(怎么可能 be,因为该字典从未传递给有问题的exec
?!) -allowed_builtins
字典当然是受影响的字典,因为它是字典作为“全局字典”传递,并且正在执行exec
的字符串中有一个global
语句!The "local" dictionary is where all names are being set during an
exec
oreval
; the "global" one is used for lookup of names not found in the "local" one, but names aren't set there unless you'reexec
ing code that includes aglobal
statement.No module object is created intrinsically by either
eval
orexec
, nor is any function object, anonymous or otherwise (again, of course: unless youexec
statements such asdef
, etc).Edit: for example, given the OP's code, and assuming
_exec
is a free-standing function since the OP's giving noclass
where it could live, add at the end:and you'll see this output:
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 theexec
in question?!) -- theallowed_builtins
dictionary is of course the one affected, since it's the dict passed as the "global dictionary" and there is aglobal
statement in the string beingexec
uted!