Generators Are Not Coroutines

A "paper" (actually a post to a newsgroup/mailing list, not a peer-reviewed paper) by PythonLanguage guru TimPeters, explaining the difference between generators, coroutines and continuations.

Useful both in the context of PythonLanguage, and for programming in general.

This is an interesting post, however it is in general just plain wrong to say that "generators are not coroutines". The term "generator" has been used since at least the 1950s (e.g., random number generator), and "coroutine" was introduced around 1964, and generators are in fact a type of coroutine in the standard definitions.

What's going on here is that PythonLanguage is now introducing its own idiosyncratic terminology, so in the Python world, and only in the Python world, is there a thing called a "Python generator" (which is functionally what the rest of the world would call a kind of CoRoutine) which is not a kind of "Python coroutine" (although it, too, is functionally a thing that the rest of the world would just call a coroutine).

I despise idiosyncratic terminology; it is egocentric to the point of being mean-spirited, because it leads to endless unnecessary confusion.

They could have called PythonLanguage Coroutines some other name, like Python Transfer Routines, and owned up to the fact that both the generator and the transfer routine are a kind of CoRoutine, and all would be simple.

I kind of like the PythonLanguage functionality under discussion there, though.

A Python generator is one specific, limited form of coroutines. If they were called coroutines, confusion would arise because people coming from other languages would expect general coroutine capability, and people having only met coroutines in Python would falsely assume there was nothing more to the coroutine concept. Feel free to use the term GeneratorCoroutine? (which I've just invented) for full disambiguation ;-)

PythonLanguage isn't alone in this space though -- CsharpLanguage's iterators have similar features and limitations.

While it's true that Python generators are coroutines, they're a degenerate case lacking recursive call capabilities. It would be like referring to the EcmaScript "function(x) { return x+1; }" as belonging to the set of RecursiveFunction's; technically true, but a non-interesting degenerate case.


View edit of February 20, 2014 or FindPage with title or text search