I was wondering, is there a function in Python that returns a dict object that contains nonlocal variables used in enclosing functions? Like vars()
or locals()
for local variables or globals()
for global ones.
Update:
As thebjorn noted, nonlocal variables that are actually used in the nested function are included in the local
list. On 3.2.3 the following code
>>> def func1():
... x=33
... def func2():
... # Without the next line prints {}
... print(x)
... print(locals())
... func2()
...
>>> func1()
returns {'x': 33}
.