Discuss / Python / nonlocal的使用

nonlocal的使用

Topic source

Dokuroo

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

def createCounter(): n=0 def natrual_num(): nonlocal n #用nonlocal后内部函数不再视n为局部变量就不会屏蔽外部n=0 while True: n+=1 yield n def counter(): return next(natrual_num()) return counter


  • 1

Reply