生成器(Generators) 是更简单创建迭代器的方法.通过 yield 关键字实现
>>> def my_generator():
print("Inside my generator")
yield 'a'
yield 'b'
yield 'c'
>>> for i in my_generator():
print(i, end=' ')
Inside my generator
a b c
>>> def my_generator():
print("Inside my generator")
yield 'a'
yield 'b'
yield 'c'
>>> for i in my_generator():
print(i, end=' ')
Inside my generator
a b c
本文标题:Python : 生成器Generators
本文链接:https://www.haomeiwen.com/subject/tmenphtx.html
网友评论