Discuss / Python / 1

csm1107

#1 Created at ... [Delete] [Delete and Lock User]

1 在python3中,可以用nonlocal 关键字声明 一个变量, 表示这个变量不是局部变量空间的变量,需要向上一层变量空间找这个变量。

2 在python2中,没有nonlocal这个关键字,我们可以把闭包变量改成可变类型数据进行修改,比如列表。

def createCounter(): s = 0 def counter(): nonlocal s s = s + 1 return s return counter


  • 1

Reply