编程语言
首页 > 编程语言> > Python中nonlocal的用法

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

标签:__,Python,text,self,nonlocal,用法,print,def
来源: https://www.cnblogs.com/zx-coder/p/12031229.html