📜  如何在生成器中将停止迭代捕获为无 - Python 代码示例

📅  最后修改于: 2022-03-11 14:46:06.307000             🧑  作者: Mango

代码示例1
# creating the list
L = [1,2,3,4,5,6]
# defining the generator
def generator(l):
    for i in l: yield i
    while True: yield  None
        

g = generator(L)

print(next(g)) # 1
print(next(g)) # 2
print(next(g)) # 3
print(next(g)) # 4
print(next(g)) # 5
print(next(g)) # 6
print(next(g)) # None
print(next(g)) # None
print(next(g)) # None