比较一下,不使用
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
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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)