python - Python/ES6 生成器也是协程吗?

标签 python generator ecmascript-6 coroutine

我对Python和ECMAScript中的生成器的理解是它们比普通生成器更强大。例如,两者都允许通过 next() 将值传递回生成器,并且它们都允许从另一个生成器生成(Python 中的 yield fromyield * ES6 中的),生成器中不需要的两件事。

那么,考虑到这种扩展功能,Python 和 ES6 中实现的生成器在所有意图和目的上都与协程相同吗?有什么区别吗?

最佳答案

来自PEP 380关于产量:

A Python generator is a form of coroutine, but has the limitation that it can only yield to its immediate caller.

来自python docs on coroutines

A coroutine is a generator that follows certain conventions. For documentation purposes, all coroutines should be decorated with @asyncio.coroutine, but this cannot be strictly enforced.

生成器是有限协同例程的一种形式。 ES2015 生成器也是如此。

关于python - Python/ES6 生成器也是协程吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31892128/

相关文章:

python - 解决这个矩阵难题的更有效的解决方案?

python - 导入/包含配置 Python

python - 发电机工作奇怪

python - 我可以简化这个 "delayed"python 生成器吗?

javascript - 模板文字插值破坏空白表

javascript - 我如何导入这些 es6 导出的内容?它们是命名为导出还是默认?

python - 如何实现高效的arnold图像置乱算法?

python - 迭代生成器和列表之间的速度差异

php - 静态关键字在 PHP 生成器函数中不起作用

javascript - Reflect.ownKeys(obj) 和 Object.keys(obj) 有什么区别?