# 闭包实现计数器 # 方法1 '''def createCounter(): s=[] def counter(): s.append('a') return len(s) return counter ''' # 方法2 '''def createCounter(): a=0 def counter(): nonlocal a a=a+1 return a return counter ''' #方法3 def createCounter(): def g(): n=1 while True: yield n n=n+1 gg=g() def counter(): return next(gg) return counter
Sign in to make a reply
nstart0902