标签:标题 嵌套 def UNC 出错 print hang div turn
看上面的标题的意思是,函数还能套函数?
name = "Alex" def change_name(): name = "Alex2" def change_name2(): name = "Alex3" print("第3层打印",name) change_name2() #调用内层函数 print("第2层打印",name) change_name() print("最外层打印",name)
此时,在最外层调用change_name2()出错
作用域在定义函数时就已经固定住了,不会随着调用位置的改变而改变
例一: name=‘alex‘ def foo(): name=‘lhf‘ def bar(): print(name) return bar func=foo() func() 例二: name=‘alex‘ def foo(): name=‘lhf‘ def bar(): name=‘wupeiqi‘ def tt(): print(name) return tt return bar func=foo() func()()
标签:标题 嵌套 def UNC 出错 print hang div turn
原文地址:https://www.cnblogs.com/hui147258/p/10855764.html