本文教程操作环境: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