[Python-ideas] Cofunctions: It's alive! Its alive!

Ron Adam rrr at ronadam.com
Wed Aug 11 06:39:07 CEST 2010

On 08/10/2010 09:48 PM, Nick Coghlan wrote:
> On Wed, Aug 11, 2010 at 10:49 AM, Greg Ewing
> <greg.ewing at canterbury.ac.nz>  wrote:
>>> - The syntax worries me. Your PEP suggests that cocall binds tightly
>>> to an atom. That would mean that if the cofunction is really a
>>> comethod, you'd have to parenthesis it,
>> No, if you examine the grammar in the PEP you'll see that
>> the atom can be followed by a subset of the trailers allowed
>> after atoms in other contexts, so it's possible to write
>> things like
>>    x = cocall foo.blarg[42].stuff(y)
>> which parses as
>>    x = cocall (foo.blarg[42].stuff)(y)
> I would expect the grammatical rules for cocall expressions to be
> similar to those for yield expressions. And if they weren't, I'd want
> to hear a really good excuse for the inconsistency :)
> Also, a possible trick to make a @cofunction decorator work:
> class cofunction:
>      # Obviously missing a bunch of stuff to tidy up the metadata
>      def __init__(self, f):
>          self._f = f
>      def __cocall__(*args, **kwds):
>          self, *args = *args
>          return yield from self._f(*args, **kwds)
> Cofunctions then wouldn't even *have* a __call__ slot, so you couldn't
> call them normally by mistake, and ordinary functions wouldn't define
> __cocall__ so you couldn't invadvertently use them with the new
> keyword.

I was wondering if a class would work.

Could using conext() and cosend() methods instead of next() and send() give 
the better error messages and distinguish cofunctions from generators 
without the need for __cocall__ ?

Can that be done with a decorator or class?


More information about the Python-ideas mailing list