用生成器来练习
Topic source@今天也是蓝色的 问题在于作用域~ it = g() return next(g()) it 这个迭代器的作用范围在counter的外面,延长了他的生存周期 如果写在里面比如next(g()) 就会每次都弹出都调用 等于都重置一份
def createCounter(): n = 0 def g(): nonlocal n while True: n = n + 1 yield n it=g() return next(it) 为什么这样每次执行出来都是1呢
- 1
用户6451503846