[Python-ideas] Cofunctions - Getting away from the iterator protocol

Greg Ewing greg.ewing at canterbury.ac.nz
Mon Oct 31 09:42:43 CET 2011


Thinking about how to support cofunctions iterating over
generators that are themselves cofunctions and therefore
suspendable, I've come to realise that cofunctionness
and generatorness really need to be orthogonal concepts.
As well as functions and cofunctions, we need generators
and cogenerators.

And thinking about how to allow *that* while building
on the yield-from mechanism, I found myself inventing
what amounts to a complete parallel implementation of
the iterator protocol, in which cogenerators create
coiterators having a __conext__ method that raises
CoStopIteration when finished, etc, etc... :-\

At which point I thought, well, why not forget about
using the iterator protocol as a basis altogether, and
design a new protocol specifically designed for the
purpose?

About then I also remembered a thought I had in passing
earlier, when Nick was talking about the fact that,
when using yield-from, there is no object available that
can hold a stack of suspended generators, so you end
up traversing an ever-lengthening chain of generator
frames as the call stack gets deeper.

However, with cofunctions there *is* a place where we
could create such an object -- it could be done by
costart(). We just need to allow it to be passed to
the places where it's needed, and with a brand-new
protocol we have the chance to do that.

I'll give a sketch of what such a protocol might be
like in my next post.

-- 
Greg





More information about the Python-ideas mailing list