代码
g=((i,i**2) if i<5 else None for i in range(6))
while t:=next(g):
print(t)
def f():
for c in 'abcde':
yield c
yield None
g=f()
while c:=next(g):
print(c)
class gen:
n,s=5,'一二三四五'
def __init__(self):
self.i=0
def __next__(self):
r=gen.s[self.i] if self.i<gen.n else None
self.i+=1
return r
g=gen()
while c:=next(g):
print(c)
代码截图
运行截图
g=((i,i**2) if i<5 else None for i in range(6))
while t:=next(g):
print(t)
def f():
for c in 'abcde':
yield c
yield None
g=f()
while c:=next(g):
print(c)
class gen:
n,s=5,'一二三四五'
def __init__(self):
self.i=0
def __next__(self):
r=gen.s[self.i] if self.i<gen.n else None
self.i+=1
return r
g=gen()
while c:=next(g):
print(c)
代码截图
运行截图