python中yield from怎么用?
十一
2021-03-05 09:56:255331浏览 · 0收藏 · 0评论
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
yield from本质:
for item in iterable: yield item的缩写版
代码示例:
def g(x): yield from range(x , 0, -1) yield from range(x) print(list(g(5)))
利用yield from语句向生成器(协程)传送数据:
def writer():
while True:
w = (yield)
print('>>', w)
def writer_wrapper(coro1):
coro1.send(None)
while True:
try:
x = (yield)
coro1.send(x)
except StopIteration:
pass
def writer_wrapper(coro2):
yield from coro2
关注公众号,随时随地在线学习
十一
认证0级讲师