nonlocal
和 global
也很容易混淆。简单记录下本身的理解。python
总之一句话,做用域是全局的,就是会修改这个变量对应地址的值。程序员
global
语句是一个声明,它适用于整个当前代码块。 这意味着列出的标识符将被解释为全局变量。 尽管自由变量可能指的是全局变量而不被声明为全局变量。闭包
global
语句中列出的名称不得用于该全局语句以前的文本代码块中。函数
global
语句中列出的名称不能定义为形式参数,也不能在 for
循环控制目标、class
定义、函数定义、import
语句或变量注释中定义。code
当前的实现并不强制执行这些限制,可是程序不该该滥用这种自由,由于将来的实现可能会强制执行这些限制,或者悄悄地改变程序的含义。对象
程序员注意:
global
是指向解析器的指令。 它仅适用于与全局语句同时解析的代码。 特别是,包含在提供给内置exec()
函数的字符串或代码对象中的全局语句不会影响包含函数调用的代码块,并且这种字符串中包含的代码不会受包含函数调用的代码中的全局语句的影响。eval()
和compile()
函数也是如此。blog
只在闭包里面生效,做用域就是闭包里面的,外函数和内函数都影响,可是闭包外面不影响。图片
nonlocal
语句使列出的标识符引用除global
变量外最近的封闭范围中的之前绑定的变量。 这很重要,由于绑定的默认行为是首先搜索本地名称空间。 该语句容许封装的代码将变量从新绑定到除全局(模块)做用域以外的本地做用域以外。作用域
nonlocal
语句中列出的名称与global
语句中列出的名称不一样,它们必须引用封闭范围中已经存在的绑定(没法明确肯定应在其中建立新绑定的范围)。字符串
没有用 nonlocal
和 global
x = 0 def outer(): x = 1 def inner(): x = 2 print("inner:", x) inner() print("outer:", x) outer() print("global:", x) # inner: 2 # outer: 1 # global: 0
nonlocal
的做用范围
x = 0 def outer(): x = 1 def inner(): nonlocal x x = 2 print("inner:", x) inner() print("outer:", x) outer() print("global:", x) # inner: 2 # outer: 2 # global: 0
global
的做用范围
x = 0 def outer(): x = 1 def inner(): global x x = 2 print("inner:", x) inner() print("outer:", x) outer() print("global:", x) # inner: 2 # outer: 1 # global: 2
global
,就不能在再声明为nonlocal
x = 0 def outer(): global x x = 1 def inner(): nonlocal x x = 2 print("inner:", x) inner() print("outer:", x) outer() print("global:", x) # SyntaxError: no binding for nonlocal 'x' found
nonlocal
以前须要初始化变量x = 0 def outer(): def inner(): nonlocal x x = 2 print("inner:", x) inner() print("outer:", x) outer() print("global:", x) # SyntaxError: no binding for nonlocal 'x' found
nonlocal
x = 0 def outer(): x = 1 nonlocal x def inner(): x = 2 print("inner:", x) inner() print("outer:", x) outer() print("global:", x) # SyntaxError: name 'x' is assigned to before nonlocal declaration
若是你感兴趣能够关注公众号「chasays」- 程序员汇聚地