[Python-ideas] Revised revised revised PEP on yield-from

Guillaume Chereau charlie137 at gmail.com
Tue Feb 17 04:05:09 CET 2009

I think we can already create coroutines and lightweight tasks already
without the need of the "yield from" syntax. The trick is to assume
that when a coroutine yields an other coroutine, the behaviour is the
same as using "yield from".
The code somehow looks like this :

# This is the actual coroutine we want to write

def func():
    yield func2()  # just like 'yield from func2()'
    yield 10  # That would be the return value in a coroutine

# This is the code -probably wrong, and missing many things, but that
gives the idea- of the coroutine library

class Coroutine:
    def __init__(generator):
        self.generator = generator
    def run(self):
        for value in self.generator:
            if not isinstance(value, Coroutine):
                yield value
                # XXX: should also do all the proper checking as 'yield from' do
                for subvalue in value.run():
                    yield subvalue

def coroutine(func):
    def ret():
        return Coroutine(func)
    return ret

The proposal could make things faster, and also slightly more coherent.
But what I don't really like with it, is that when you start to write
coroutines, you have to use yield every time you call an other
coroutine, and it make the code full of yield statement ; the proposal
if adopted would make it even worst.

Guillaume Chereau


More information about the Python-ideas mailing list