我对Python和ECMAScript中的生成器的理解是它们比普通生成器更强大。例如,两者都允许通过 next()
将值传递回生成器,并且它们都允许从另一个生成器生成(Python 中的 yield from
和 yield * 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.
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/