[Python-ideas] Are we hammering thumbtacks? [was: Re: x=(yield from) confusion]

Jim Jewett jimjjewett at gmail.com
Mon Apr 6 01:16:03 CEST 2009

On Fri, Apr 3, 2009 at 8:32 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> That is, yield, send() and throw() involve communication between the
> currently active subcoroutine and the client of the whole coroutine.
> They bypass the current stack in the coroutine itself. The return
> values, on the other hand, *do* involve unwinding the coroutine stack,
> just like they do with normal function calls.
>  # Note how similar this is to the normal version above
>  def average_diff_cr(start):
>    avg1 = yield from average_cr(start, seq1)
>    avg2 = yield from average_cr(start, seq2)
>    return avg2 - avg1

Yes, this explanation and example finally made that clear to me.

But it also makes me wonder if we aren't using the wrong tool.

It seems that you want the yields to pass opaquely through
average_diff -- possibly without any actual value; just a
go-ahead-and-suspend-me flag.

Meanwhile, you do want some values to be shared between
average_diff and average_cr.  average_diff can send in some
initial values when it creates/initializes average_cr, but there
isn't any way to get information back without hijacking the
stream of yields.

The proposals below are more ambitious than the current PEP,
but they still somehow feel less contorted by special cases:

Option 1:  Recognize that yield is serving two purposes, and
find another way to spell go-ahead-and-suspend-me.  It may
be too late to do this cleanly.

Option 2:  Find another way for average_diff and average_cr
to share information.

    Option 2a:  Add anoher set of methods, similar to send,
    but specific to co-routines.  This starts to look like the
    Actor that Bruce Eckel asked about a a while ago, and
    Kamaelia may have a good pattern.

    Option 2b:  Let the caller and callee share a scope,
    similar to exec in locals. (Previous discussions talked
    about thunks or macros.)


More information about the Python-ideas mailing list