qq_遁去的一_1
比较这一点,而不使用nonlocal: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,在哪里inner()氏x现在也是outer()氏x: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正确的“全局”值: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