atom feed24 messages in org.python.python-ideas[Python-ideas] Cofunctions - an enhan...
FromSent OnAttachments
Greg EwingAug 1, 2010 3:04 am 
gha...@gmail.comAug 1, 2010 4:52 am 
Antoine PitrouAug 1, 2010 6:20 am 
Greg EwingAug 1, 2010 5:06 pm 
Greg EwingAug 1, 2010 5:13 pm 
Antoine PitrouAug 1, 2010 5:16 pm 
gha...@gmail.comAug 1, 2010 5:30 pm 
Greg EwingAug 1, 2010 10:57 pm 
Bruce LebanAug 1, 2010 11:29 pm 
Greg EwingAug 2, 2010 2:27 am 
Nick CoghlanAug 2, 2010 5:03 am 
Scott DialAug 2, 2010 9:21 am 
Antoine PitrouAug 2, 2010 9:37 am 
Guido van RossumAug 2, 2010 9:39 am 
Guido van RossumAug 2, 2010 9:45 am 
Greg EwingAug 2, 2010 6:28 pm 
Greg EwingAug 2, 2010 6:46 pm 
Gregory EwingAug 2, 2010 11:12 pm 
gha...@gmail.comAug 2, 2010 11:16 pm 
Guido van RossumAug 3, 2010 7:11 am 
Greg EwingAug 3, 2010 3:53 pm 
Cameron SimpsonAug 5, 2010 3:31 pm 
Jack DiederichAug 5, 2010 5:14 pm 
Greg EwingAug 6, 2010 12:31 am 
Subject:[Python-ideas] Cofunctions - an enhancement to yield-from
From:Greg Ewing (
Date:Aug 1, 2010 3:04:33 am

I've been thinking about this idea for a while, and now that I've got yield-from nailed down to my satisfaction, I thought I'd put it out there to see if anyone else thinks it would be a good thing.



A drawback of 'yield from' as a coroutine mechanism is that it must be used every time a function forming part of a coroutine calls another function that can suspend the coroutine, either directly or indirectly.

This makes the code read somewhat awkwardly and provides many opportunities for the programmer to make errors. It also introduces considerable coupling, since changing one's mind about whether a function needs to be suspendable requires revisiting all the call sites of that function.

This proposal builds on the 'yield from' proposal by introducing a new kind of function that I will call a "cofunction".

A cofunction is a special kind of generator, with the following characteristics:

- It is defined by using the keyword 'codef' in place of 'def'.

- It is always a generator, even if it does not contain any yields.

- Whenever a call is made inside a cofunction, it is done using a special COCALL opcode. This first looks for a __cocall__ method on the object being called. If present, it is expected to return an iterable object, which is treated as though 'yield from' had been performed on it.

If the object being called does not have a __cocall__ method, or it returns NotImplemented, the call is made in the usual way through the __call__ method.

- Cofunctions themselves have a __cocall__ method that does the same thing as __call__.

Using these cofunctions, it should be possible to write coroutine code that looks very similar to ordinary code. Cofunctions can call both ordinary functions and other cofunctions using ordinary call syntax. The only special consideration is that 'codef' must be used to define any function that directly or indirectly calls another cofunction.

A few subtle details:

- Ordinary generators will *not* implement __cocall__. This is so that a cofunction can e.g. contain a for-loop that iterates over a generator without erroneously triggering yield-from behaviour.

- Some objects that wrap functions will need to be enhanced with __cocall__ methods that delegate to the underlying function. Obvious ones that spring to mind are bound methods, staticmethods and classmethods.

Returning NotImplemented is specified as one of the possible responses of __cocall__ so that a wrapper can report that the wrapped object does not support __cocall__.