<分区>
我想知道,Python 中是否有一个函数返回一个包含封闭函数中使用的非局部变量的 dict 对象?像局部变量的 vars()
或 locals()
或全局变量的 globals()
。
更新:
正如 thebjorn 所指出的,在嵌套函数中实际使用 的非局部变量包含在 local
列表中。关于3.2.3以下代码
>>> def func1():
... x=33
... def func2():
... # Without the next line prints {}
... print(x)
... print(locals())
... func2()
...
>>> func1()
返回 {'x': 33}
。