为什么在字典理解中使用eval
访问函数参数会失败?
ARGS1 = ('a1', 'b1')
def foo1(a1, b1):
return {arg:eval(arg) for arg in ARGS1}
print foo1("A1", "B1") # NameError: name 'a1' is not defined
列表理解中的同样的事情很好:
ARGS2 = ('a2', 'b2')
def foo2(a2, b2):
return [eval(arg) for arg in ARGS2]
print foo2("A2", "B2") # OK, print: ['A2', 'B2']
如果没有功能,它也可以正常运行:
ARGS3 = ('a3', 'b3')
a3, b3 = ("A3", "B3")
print {arg:eval(arg) for arg in ARGS3} # OK, print: ['A3', 'B3']
或者如果定义了全局变量:
ARGS4 = ('a4', 'b4')
a4, b4 = ("A4", "B4")
def foo4():
return [eval(arg) for arg in ARGS4]
print foo4() # OK, print: ['A4', 'B4']
这看起来真的像个错误,但也许我在这里错过了一些东西。
(已编辑为包含无功能& with-globals示例)
答案 0 :(得分:5)
dict理解在新范围内执行,如函数。
因此,表达式locals仅限于只是在循环中命名的那些,在本例中为arg
。父函数本地是不考虑的,因为闭包在编译时仅 绑定。 eval()
引用的名称无法使用闭包。
以下也不起作用:
>>> ARGS = ('a', 'b')
>>> def bar(a, b):
... def foo():
... for arg in ARGS:
... eval(arg)
... return foo
...
>>> print bar("A", "B")()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in foo
File "<string>", line 1, in <module>
NameError: name 'a' is not defined
内部a
函数无法使用名称b
和foo
,除非编译器确定该函数确实需要访问它们:
>>> def bar2(a, b):
... def foo():
... a, b
... for arg in ARGS:
... eval(arg)
... return foo
...
>>> print bar2("A", "B")()
None
>>> print bar2("A", "B").func_closure
(<cell at 0x1051bac20: str object at 0x104613328>, <cell at 0x1051bacc8: str object at 0x1045971e8>)
>>> print bar2("A", "B").__code__.co_freevars
('a', 'b')
这里,行a, b
只能引用父作用域本地(它们未在foo()
本身中分配),因此编译器为这些创建了闭包,并且名称已经可用作为 foo()
内的本地人。
Python 2中的列表推导没有自己的命名空间,在Python 3中更正了一个遗漏,并没有扩展到dict和set comprehension。见Python list comprehension rebind names even after scope of comprehension. Is this right?