作业_返回函数
Topic source我思路和你一样:
def createCounter(): def natural_int(): i = 1 while True: yield i i+=1
#it = natural_int()
def counter():
return next(natural_int())
return counter
可是这样就错了,为啥,全是1
个人理解: return next(natural_int())这里natural_int()只是临时的generator,调用后就会销毁。return next(i)的话i已经在counter()外部定义了,可以一直使用。
- 1
落之萧萧
def createCounter(): def b(): n = 0 while True: n += 1 yield n i = b() def counter(): return next(i) return counter