global 语句是一个声明,它适用于全体当前代码块。 这意味着列出的标识符将被阐明为全局变量。 只管自由变量可能指的是全局变量而不被声明为全局变量。
global语句中列出的名称不得用于该全局语句之前的文本代码块中。
global语句中列出的名称不能定义为形式参数,也不能在 for 循环掌握目标、class定义、函数定义、import语句或变量注释中定义。

当前的实现并不逼迫实行这些限定,但是程序不应该滥用这种自由,由于未来的实现可能会逼迫实行这些限定,或者悄悄地改变程序的含义。
程序员把稳: global是指向解析器的指令。 它仅适用于与全局语句同时解析的代码。 特殊是,包含在供应给内置 exec()函数的字符串或代码工具中的全局语句不会影响包含函数调用的代码块,而且这种字符串中包含的代码不会受包含函数调用的代码中的全局语句的影响。 eval()和 compile()函数也是如此。
nonlocal只在闭包里面生效,浸染域便是闭包里面的,外函数和内函数都影响,但是闭包表面不影响。
nonlocal 语句使列出的标识符引用除global变量外最近的封闭范围中的以前绑定的变量。 这很主要,由于绑定的默认行为是首先搜索本地名称空间。 该语句许可封装的代码将变量重新绑定到除全局(模块)浸染域之外确当地浸染域之外。
nonlocal语句中列出的名称与global语句中列出的名称不同,它们必须引用封闭范围中已经存在的绑定(无法明确确定应在个中创建新绑定的范围)。
举例
没有用 nonlocal 和 global
x = 0def 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 = 0def 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 = 0def 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,就不能在再声明为nonlocalx = 0def 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 = 0def 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
---
不能在函数的外部函数里面声明nonlocalx = 0def 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」- 程序员汇聚地