def test_next():
for i in range(10):
yield i
t = test_next()
next(t)
0
next(t)
1
next(t)
2
next(t)
yield 是生成器相当于return的功能,第二种就是中断在那,用next的把值拿出来,但是生成器就是可以按照需求拿数目节约cpu
def test_next():
for i in range(10):
yield i
t = test_next()
next(t)
0
next(t)
1
next(t)
2
next(t)
yield 是生成器相当于return的功能,第二种就是中断在那,用next的把值拿出来,但是生成器就是可以按照需求拿数目节约cpu