nonlocal在Python 3.x中做什么?
关闭调试问题,OP需要非局部的,并没有意识到它,请使用Is it possible To modify variable in python that Is outer, but not global, scope?代替。
虽然Python 2在2020年1月1日正式不支持,但如果出于某种原因,您被迫维护Python 2。并且需要与nonlocal等价的,请参见Python 2.x中的nonlocal关键字。
nonlocal在Python 3.x中做什么?
关闭调试问题,OP需要非局部的,并没有意识到它,请使用Is it possible To modify variable in python that Is outer, but not global, scope?代替。
虽然Python 2在2020年1月1日正式不支持,但如果出于某种原因,您被迫维护Python 2。并且需要与nonlocal等价的,请参见Python 2.x中的nonlocal关键字。
当前回答
比较一下,不使用nonlocal:
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
为此,使用非局部,其中inner()的x现在也是outer()的x:
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绑定到正确的“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
其他回答
使用“非局部”内部函数(即嵌套内部函数)可以获得外部父函数的特定变量的读和写权限。而nonlocal只能在内部函数中使用 例如:
a = 10
def Outer(msg):
a = 20
b = 30
def Inner():
c = 50
d = 60
print("MU LCL =",locals())
nonlocal a
a = 100
ans = a+c
print("Hello from Inner",ans)
print("value of a Inner : ",a)
Inner()
print("value of a Outer : ",a)
res = Outer("Hello World")
print(res)
print("value of a Global : ",a)
我个人对“非局部”语句的理解(请原谅我对Python和编程并不熟悉)是,“非局部”是在迭代函数中使用全局功能的一种方式,而不是在代码本身中使用。一个函数之间的全局语句。
a = 0 #1. global variable with respect to every function in program
def f():
a = 0 #2. nonlocal with respect to function g
def g():
nonlocal a
a=a+1
print("The value of 'a' using nonlocal is ", a)
def h():
global a #3. using global variable
a=a+5
print("The value of a using global is ", a)
def i():
a = 0 #4. variable separated from all others
print("The value of 'a' inside a function is ", a)
g()
h()
i()
print("The value of 'a' global before any function", a)
f()
print("The value of 'a' global after using function f ", a)
它取源代码中“最接近”的引用点。 这被称为“词法范围”,是>40年来的标准。
Python的类成员实际上在一个名为__dict__的字典中,并且永远不会通过词法作用域到达。
如果你不指定nonlocal,而是指定x = 7,它将创建一个新的局部变量"x"。 如果你指定了nonlocal,它会找到“最接近”的“x”并赋值给它。 如果你指定了nonlocal并且没有“x”,它会给你一个错误消息。
关键字global对我来说总是很奇怪,因为它会很高兴地忽略所有其他的“x”,除了最外面的那个。
引用自Python 3
非局部语句导致列出的标识符引用之前在最近的封闭范围内绑定的变量(不包括全局变量)。
如参考文献中所述,在多个嵌套函数的情况下,只有最近的封闭函数中的变量被修改:
def outer():
def inner():
def innermost():
nonlocal x
x = 3
x = 2
innermost()
if x == 3: print('Inner x has been modified')
x = 1
inner()
if x == 3: print('Outer x has been modified')
x = 0
outer()
if x == 3: print('Global x has been modified')
# Inner x has been modified
“最近的”变量可能在几层之外:
def outer():
def inner():
def innermost():
nonlocal x
x = 3
innermost()
x = 1
inner()
if x == 3: print('Outer x has been modified')
x = 0
outer()
if x == 3: print('Global x has been modified')
# Outer x has been modified
但它不能是一个全局变量:
def outer():
def inner():
def innermost():
nonlocal x
x = 3
innermost()
inner()
x = 0
outer()
if x == 3: print('Global x has been modified')
# SyntaxError: no binding for nonlocal 'x' found