我尝试在函数的局部作用域中使用eval()。然而,它总是在全局范围内进行评估。在
独立示例:
1-本规范适用:
var1 = 1
var2 = 2
var3 = 3
myDict = dict((name, eval(name)) for name in ["var1",
"var2",
"var3"])
print(myDict["var1"])
2-为lvar1
抛出NameError
3-结果与2相同。在
def test2():
lvar1 = 1
lvar2 = 2
lvar3 = 3
myDict = dict((name, eval(name), locals()) for name in ["lvar1",
"lvar2",
"lvar3"])
print(myDict["lvar1"])
首先,重要的是read this:
首先要注意的是生成器表达式有自己的作用域(对于dict理解也是如此),因此它有自己的
locals()
字典。在这是因为在全局范围内,}dict都指向同一个字典,因此dict构造函数可以访问这些变量。
globals()
和{这里我们再次调用
eval()
,没有globals()
和locals()
dict,因此它最终使用全局作用域和它自己的局部作用域(它是空的),并且在这些作用域中没有这样的变量可用。记住生成器有自己的作用域,所以在这里调用
locals()
几乎没有任何区别,这是一个空的dict。解决方案:
这是因为我们在一个变量中捕获了test1的
locals()
,然后在字典理解中使用了该字典,所以它现在可以访问这些变量。在保存
locals()
(或vars()
)调用的结果以返回函数的局部作用域。否则,生成器表达式中的locals()
将返回gen expr的局部作用域。在顺便说一句,你不需要一个明确的理解来建立这句话:
^{pr2}$相关问题 更多 >
编程相关推荐