生成器函数的定义
举例说明
def add():
for i in range(10):
yield i
g = add()
print(g) # <generator object add at 0x10f6110f8>
print(next(g)) # 0
print(next(g)) # 1
带语法糖的生成器函数
def add2():
yield from range(10)
def add():
for i in range(10):
yield i
g = add()
print(g) # <generator object add at 0x10f6110f8>
print(next(g)) # 0
print(next(g)) # 1
def add2():
yield from range(10)
本文标题:python 生成器函数
本文链接:https://www.haomeiwen.com/subject/ycgoyhtx.html
网友评论