【问题标题】:Are there still coroutines in Python 3?Python 3 中还有协程吗?
【发布时间】:2015-02-26 07:55:35
【问题描述】:

这里http://www.dabeaz.com/coroutines/有Python 2中协程的详细解释。但是,在 Python 3 official documentation 中搜索关键字“coroutine”和“(yield)”时,我发现只有 asyncio.coroutines 特定于 asyncio 模块并且它们使用“yield from”语法。

【问题讨论】:

标签: python-3.x yield coroutine


【解决方案1】:

是的。 Beazley 写的所有内容都继续适用于 Python 3。

注意。在 2.7 yield statement 文档中,“协程”仅出现在另见 PEP 0342 标题中。对于 3.x,带有该链接的 See also 部分已移至 previous chapter 中的 yield 表达式 部分。 3.x yield statement 文档有 (yield <expr>) 而不仅仅是 (yield)

【讨论】:

  • 谢谢,现在我在PEP 342看到了协程的详细官方解释。据我了解,生成器(产生值)使用“yield x”语法,而协程(使用值)使用“x = yield”语法。在 Python 2 中,他们使用“x = (yield)”语法,在 Python 3 中,“x = yield”和“x = (yield)”都是可能的。如果有人在 python 中提供协程的最常见用法,那也很棒吗? Beazley 谈到将它们用作管道和简单的状态机......
  • stdlib 中协程的唯一用途是在 asyncio 中,使用新的 yield from 语法。 Beazley 在 2014 年做了一个新的演讲,其中包括这个。
猜你喜欢
  • 2020-05-24
  • 2019-03-16
  • 2022-12-05
  • 2019-04-29
  • 1970-01-01
  • 1970-01-01
  • 2010-12-11
  • 2019-04-11
  • 1970-01-01
相关资源
最近更新 更多