1

My understanding of the generators in Python and ECMAScript is that they are more capable than ordinary generators. For example, both allow for values to passed back into the generator via next(), and they both allow yielding from another generator (yield from in Python and yield * in ES6), two things that aren't needed in generators.

So, given this extended functionality, are generators as implemented in Python and ES6 for all intents and purposes the same as coroutines? Are there any differences?

Amit
  • 45,440
  • 9
  • 78
  • 110
Thomas Foster
  • 1,303
  • 1
  • 10
  • 23
  • 1
    Notice that yielding from another generator is not really a feature; all generators can do this. `yield*` is just syntactic sugar. – Bergi Aug 08 '15 at 18:30
  • possible duplicate of [Coroutine vs Continuation vs Generator](http://stackoverflow.com/q/715758/1048572) – Bergi Aug 08 '15 at 18:32

1 Answers1

2

From the PEP 380 on yield from:

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

From the 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.

Generators are a form of limited co-routine. The same should be true for the ES2015 generators.

Kit Sunde
  • 35,972
  • 25
  • 125
  • 179