[Python-Dev] Coroutines, generators, function calling

Michel Pelletier michel at cignex.com
Thu Oct 20 00:14:31 CEST 2005


Andrew Koenig wrote:
>>  Sure, that would work.  Or even this, if the scheduler would
>>automatically recognize generator objects being yielded and so would run
>>the the nested coroutine until finish:
> 
> 
> This idea has been discussed before.  I think the problem with recognizing
> generators as the subject of "yield" statements is that then you can't yield
> a generator even if you want to.
> 
> The best syntax I can think of without adding a new keyword looks like this:
> 
> 	yield from x
> 
> which would be equivalent to
> 
> 	for i in x:
> 	    yield i

My eyes really like the syntax, but I wonder about it's usefulness.  In 
rdflib, particularly here:

http://svn.rdflib.net/trunk/rdflib/backends/IOMemory.py

We yield values from inside for loops all over the place, but the 
yielded value is very rarely just the index value (only 1 of 14 yields) 
, but something calculated from the index value, so the new syntax would 
not be useful, unless it was something that provided access to the index 
item as a variable, like:

yield foo(i) for i in x

which barely saves you anything (a colon, a newline, and an indent). 
(hey wait, isn't that a generator comprehension?  Haven't really 
encountered those yet). Of course rdflib could be the minority case and 
most folks who yield in loops are yielding only the index value directly.

off to read the generator comprehension docs...

-Michel


More information about the Python-Dev mailing list