为什么 exec 中的闭包会被破坏?
在 Python 2.6 中,
>>> exec "print (lambda: a)()" in dict(a=2), {}
2
>>> exec "print (lambda: a)()" in globals(), {'a': 2}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
File "<string>", line 1, in <lambda>
NameError: global name 'a' is not defined
>>> exec "print (lambda: a).__closure__" in globals(), {'a': 2}
None
我希望它打印 2
两次,然后使用单个 cell
打印一个元组。 3.1中也是同样的情况。这是怎么回事?
In Python 2.6,
>>> exec "print (lambda: a)()" in dict(a=2), {}
2
>>> exec "print (lambda: a)()" in globals(), {'a': 2}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
File "<string>", line 1, in <lambda>
NameError: global name 'a' is not defined
>>> exec "print (lambda: a).__closure__" in globals(), {'a': 2}
None
I expected it to print 2
twice, and then print a tuple with a single cell
. It is the same situation in 3.1. What's going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您将字符串传递给
exec
或eval
时,它会在考虑全局变量或局部变量之前将该字符串编译为代码对象。因此,当您说:这意味着:
compile
无法知道a
是一个 freevar,因此它将其编译为全局引用:因此要使其工作,您必须将
a
放入全局变量中,而不是局部变量中。是的,这有点狡猾。但我想这对你来说就是 exec 和 eval ......它们不应该是好的。
When you pass a string to
exec
oreval
, it compiles that string to a code object before considering globals or locals. So when you say:it means:
There's no way for
compile
to know thata
is a freevar, so it compiles it to a global reference:Therefore to make it work you have to put
a
in the globals and not the locals.Yeah, it's a bit dodgy. But then that's
exec
andeval
for you I suppose... they're not supposed to be nice.