Monday, December 19, 2005

Daddy, What Is A Coroutine?

Some people have gotten the idea that Python 2.5 will have coroutines.

Let me just be blunt. Python 2.5 will not have coroutines. PEP 342 even says so. Coroutines can be switched between regardless of their position in the call stack. Python's generators can only switch to the frame directly above them in the stack. The PEP suggests that it will allow coroutines to be implemented using a trampoline (with which, by the way, Guido doesn't even think anyone important will bother). Well, this may be true in the strictest sense - PEP 342 won't prevent you from implementing such a trampoline, but it is already possible to do so.

Don't misinterpret: PEP 342 corrects a long-standing mistake in Python's generators (yield definitely makes sense as an expression). Insofar as it corrects a syntactic inequity in the language, this is a fine PEP. It just has nothing to do with coroutines.

1 comment:

  1. I know this is old, but...

    Yeah, I agree generators aren't coroutines.

    But I think PEP 342 is a mistake. 'yield' is a control structure, and as such doesn't make much sense to me as an expression. It's only a little better than 'raise' or 'return' being expressions.

    Passing information into a generator in this way doesn't make a lot of sense to me, either. It makes more sense to just give it a queue (or something) as a parameter. (Admittedly that would be even more sensible if Python had real coroutines.)