#函数的作用域只跟函数声明时定义的作用域有关,跟函数的调用位置无任何关系
name = 'alex' def foo(): name='linhaifeng' def bar(): #name='wupeiqi' print(name) return bar a=foo() print(a) a() #bar() 结果: <function foo.<locals>.bar at 0x7f7956a22158> linhaifeng
name='alex' def foo(): name='lhf' def bar(): name='wupeiqi' print(name) def tt(): print(name) return tt return bar bar=foo() tt=bar() print(tt) tt() 结果: wupeiqi <function foo.<locals>.bar.<locals>.tt at 0x7f1ee6c911e0> wupeiqi