生成器(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