yield_all needed in Python

Nick Coghlan ncoghlan at iinet.net.au
Wed Mar 2 07:54:14 EST 2005


Douglas Alan wrote:
> Steve Holden <steve at holdenweb.com> writes:
> 
> 
>>Guido has generally observed a parsimony about the introduction of
>>features such as the one you suggest into Python, and in particular
>>he is reluctant to add new keywords - even in cases like decorators
>>that cried out for a keyword rather than the ugly "@" syntax.
> 
> 
> In this case, that is great, since I'd much prefer
> 
>    yield *gen1(arg)

If you do write a PEP, try to get genexp syntax supported by the yield keyword.

That is, the following currently triggers a syntax error:
   def f():
     yield x for x in gen1(arg)

I wouldn't mind seeing it successively yield the values returned by gen1() 
instead. To my mind that better conveys what's going on than putting the yield 
statement inside the for loop (it should also provide the opportunity to 
optimise the next() calls by temporarily swapping the outer generator's frame 
for the inner generator's frame, and swapping them back only when the inner 
generator is exhausted)

That syntax error I mentioned makes it backwards compatible, too (existing code 
must have parentheses around the genexp, which will not by altered if the yield 
keyword gains a native genexp syntax).

If anyone thinks that this would result in a couple of parentheses making too 
much difference, consider this:

Py> [x for x in []]
[]
Py> [(x for x in [])]
[<generator object at 0x009E6698>]

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list