Python:从lambda中访问一个在作用域中但不在命名空间中的名称

Python:从lambda中访问一个在作用域中但不在命名空间中的名称,python,lambda,namespaces,scope,execfile,Python,Lambda,Namespaces,Scope,Execfile,考虑以下代码: aDict = {} execfile('file.py',globals(),aDict) aDict['func2']() # this calls func2 which in turn calls func1. But it fails file.py包含以下内容: def func1(): return 1 myVar = func1() # checking that func1 exists in the scope func2 = lambd

考虑以下代码:

aDict = {}    
execfile('file.py',globals(),aDict)
aDict['func2']() # this calls func2 which in turn calls func1. But it fails
file.py包含以下内容:

def func1():
    return 1

myVar = func1() # checking that func1 exists in the scope

func2 = lambda: func1()
这会出现一个错误,提示NameError:未定义全局名称“func1”

我不确定这里发生了什么。 file.py中的代码使用空的本地命名空间执行。 然后,在代码中定义了一个新函数,并立即成功地调用了该函数。这意味着该函数确实存在于该范围内

所以。。。为什么不能在lambda中调用func1

在其他语言上,lambda/闭包绑定到定义它们的范围。
Python中的规则是怎样的?它们是否受范围的约束?到名称空间?

我认为func1在file.py的名称空间中定义的名称空间已经消失,因此它无法再次查找它。 下面是记住func1的代码,尽管它很难看:

func2 = (lambda x: lambda : x())(func1)