#生成器函数 def func(): print('abc') yield 333 yield 444 ret = func() print(next(ret)) print(next(ret)) #一个next对应一个yield #return:函数中只存在一个return结束函数,并且给函数执行者返回值 #yield:只要函数中有yield那么他就是生成器函数而不是函数 #生成器函数中可以存在多个yield,yield不会结束函数 def gen_func(): for i in range(1,5000): yield f'{i}号包子' ret = gen_func() for i in range(200): print(next(ret)) for i in range(200): print(next(ret)) def func1(): li = [1,2,3,4,5,6] yield from li #将li这个列表变成了迭代器返回 ret = func1() print(next(ret)) print(next(ret)) print(next(ret)) print(next(ret)) def func2(): li1 = [1,2,3,4,5,6] li2 = [7,8,9,10,11] yield from li1 yield from li2 ret = func2() for i in ret: print(i)
标签:函数,生成器,yield,next,ret,print From: https://www.cnblogs.com/moon3496694/p/17124818.html