情况一: a 直接引用外部的,正常运行 def toplevel(): a = 5 def nested(): print(a + 2) # theres no local variable a so it prints the nonlocal one nested() return a 情况二:创建local 变量a,直接打印,正常运行 def toplevel(): a = 5 def nested(): a = 7 # create a local variable called a w…
全局变量调用:想要在自定义的函数中使用全局变量,就得要在函数用关键字global声明,然后就可以对全局变量进行修改.嵌套函数中的变量的调用:要在嵌套的变量中,使用nonlocal的声明'''num = 1def fun1(): global num # 需要使用 global 关键字声明 print(num) num = 123 print(num)fun1()print(num) def outer(): num = 10 def inner(): …