Python:如何在函数的局部作用域中运行eval()

2024-10-01 09:40:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在函数的局部作用域中使用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

^{pr2}$

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"])

Tags: 函数nameinforeval局部dictmydict
2条回答

首先,重要的是read this

The expression argument is parsed and evaluated as a Python expression (technically speaking, a condition list) using the globals and locals dictionaries as global and local namespace. If the globals dictionary is present and lacks ‘__builtins__’, the current globals are copied into globals before expression is parsed. This means that expression normally has full access to the standard __builtin__ module and restricted environments are propagated. If the locals dictionary is omitted it defaults to the globals dictionary. If both dictionaries are omitted, the expression is executed in the environment where eval() is called. The return value is the result of the evaluated expression`.

首先要注意的是生成器表达式有自己的作用域(对于dict理解也是如此),因此它有自己的locals()字典。在

  1. 这是因为在全局范围内,globals()和{}dict都指向同一个字典,因此dict构造函数可以访问这些变量。

  2. 这里我们再次调用eval(),没有globals()locals()dict,因此它最终使用全局作用域和它自己的局部作用域(它是空的),并且在这些作用域中没有这样的变量可用。

  3. 记住生成器有自己的作用域,所以在这里调用locals()几乎没有任何区别,这是一个空的dict。

解决方案:

def test1():
   lvar1 = 1
   lvar2 = 2
   lvar3 = 3
   test1_locals = locals()
   myDict = dict((name, eval(name, test1_locals)) for name in ["lvar1",
                                                 "lvar2",
                                                 "lvar3"])
   print myDict
   print(myDict["lvar1"])

这是因为我们在一个变量中捕获了test1的locals(),然后在字典理解中使用了该字典,所以它现在可以访问这些变量。在

保存locals()(或vars())调用的结果以返回函数的局部作用域。否则,生成器表达式中的locals()将返回gen expr的局部作用域。在

def test3():
    lvar1 = 1
    lvar2 = 2
    lvar3 = 3
    scope = locals()
    myDict = dict((name, eval(name, scope)) for name in [
                  "lvar1", "lvar2", "lvar3"])
    print(myDict["lvar1"])

顺便说一句,你不需要一个明确的理解来建立这句话:

^{pr2}$

相关问题 更多 >