Python中nonlocal的用法

 1 class Text:
 2     def __init__(self):
 3         pass
 4     def big(self):
 5         n, m = 0, 0
 6         def a():
 7             nonlocal n
 8             n += 1
 9             print(n)
10             if n == 1:
11                 print('one')
12             elif n == 2:
13                 print('two')
14         a()
15         a()
16 if __name__ == '__main__':
17     text = Text()
18     text.big()

打印结果:

1
one
2
two

原文地址:https://www.cnblogs.com/zx-coder/p/12031229.html