yield from用于委托生成器执行,可简化代码并实现数据透传、异常传递和双向通信。

在Python协程中,yield from 主要用于委托生成器或协程的执行,把当前生成器的操作“转发”给另一个可迭代对象或子生成器。它不仅简化了代码,还能让外层生成器直接与内层生成器通信,实现数据的透传和异常传递。
当你有一个生成器函数,想从中调用另一个生成器,并依次产出它的值,可以直接使用 yield from。
def sub_generator():
yield 1
yield 2
yield 3
<p>def main_generator():
yield from sub_generator()
yield "done"</p><h1>使用</h1><p>for value in main_generator():
print(value)</p><h1>输出:</h1><h1>1</h1><h1>2</h1><h1>3</h1><h1>done</h1>这里 yield from 把 sub_generator() 的所有产出值直接交给 main_generator 的调用者,无需手动遍历。
yield from 还能将子生成器的返回值传递给父生成器。当子生成器通过 return 返回值时,这个值会成为 yield from 表达式的返回值。
立即学习“Python免费学习笔记(深入)”;
def sub_task():
yield "step1"
yield "step2"
return "result_from_sub"
<p>def main_task():
result = yield from sub_task()
yield f"received: {result}"</p><p>for value in main_task():
print(value)</p><h1>输出:</h1><h1>step1</h1><h1>step2</h1><h1>received: result_from_sub</h1>注意:子生成器结束时的 return value 被捕获并赋给了 result,这是普通 for + yield 做不到的。
yield from 支持调用者向子生成器发送数据或抛出异常,实现完整的协程协作。
def echo_subgenerator():
while True:
try:
data = yield
if data == "quit":
return "bye"
print(f"echo: {data}")
except ValueError:
print("caught ValueError")
<p>def delegator():
result = yield from echo_subgenerator()
print(f"sub finished with: {result}")</p><p>gen = delegator()
next(gen) # 启动
gen.send("hello") # 输出: echo: hello
gen.send("world") # 输出: echo: world
gen.throw(ValueError) # 输出: caught ValueError
gen.send("quit") # 输出: sub finished with: bye</p>所有 send、throw 操作都会被 yield from 自动转发到 echo_subgenerator,调用者就像直接操作子协程。
基本上就这些。yield from 在 Python 3.4 及更早版本中是构建协程的核心工具,后来被 async/await 取代,但在理解生成器委托机制时仍很重要。
以上就是python协程里yield from如何使用?的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号