[Python-Dev] accumulator display syntax

Phillip J. Eby pje at telecommunity.com
Fri Oct 17 14:04:52 EDT 2003


At 10:15 AM 10/17/03 -0700, Guido van Rossum wrote:
> > But, indexing does stretch quite
> > far in the current Python syntax and semantics (in Python's
> > *pragmatics* you're supposed to use it far more restrainedly).
>
>Which is why I didn't like the 'sum[x for x in S]' notation much.
>Let's look for an in-line generator notation instead.  I like
>
>   sum((yield x for x in S))
>
>but perhaps we can make this work:
>
>   sum(x for x in S)

Offhand, it seems like the grammar might be rather tricky, but it actually 
does seem more Pythonic than the "yield" syntax, and it retroactively makes 
listcomps shorthand for 'list(x for x in s)'.  However, if gencomps use 
this syntax, then what does:

for x in y*2 for y in z if y<20:
     ...

mean?  ;)

It's a little clearer with parentheses, of course, so perhaps they should 
be required:

for x in (y*2 for y in z if y<20):
     ...

It would be more efficient to code that stuff inline in the loop, if the 
gencomp creates another frame, but it *looks* more efficient to put it in 
the for statement.  But maybe I worry too much, since you could slap a 
listcomp in a for loop now, and I've never even thought of doing so.




More information about the Python-Dev mailing list