[Python-Dev] Minimal 'stackless' PEP using generators?

Clark C. Evans cce at clarkevans.com
Mon Aug 23 18:34:20 CEST 2004


On Mon, Aug 23, 2004 at 11:56:04AM -0400, Phillip J. Eby wrote:
| >1.  The PEP would introduce a new 'builtin' class called 'Cooperate'
| >
| >2.  Generator semantics would be altered so that 'yield X', where X
| >    is an instance of Cooperate, would automagically propigate to
| >    the outer-most non-generator.
| 
| Perhaps you mean "inner-most"?

Yes.  The top-most non-generator on the stack.

| It doesn't seem to me to actually help anything.  You can already do this 
| using a simple wrapper object that maintains a stack of active 
| generators, as I do in 'peak.events'.

Could you provide an example?  The problem this proposal solves is 
straight-foward -- it is tedious and slow to have intermediate 
generators do stuff like:

    def middle():
    """ intermediate generator _only_ sees one and two """
    for x in top():
!       if isinstance(x,X):
!           yield x
        print "middle", x
        yield x

This extra step is tedious and also slow; especially if one has lots of
yield statements that cooperate.  It could be standardized and made a
bit snappier if it was built-in behavior.   This is an 80/5 proposal.
One gets 80% of the happiness, with 5% of the pain.

| I was hoping that you had actually come up with a solution for the more 
| complex problem of suspending *non* generator functions, in a way that 
| would work with CPython.  :(

Yes, I know.  I'm trying to avoid this much harder problem.

Clark


More information about the Python-Dev mailing list