[Python-Dev] Re: accumulator display syntax

Paul Moore pf_moore at yahoo.co.uk
Fri Oct 17 16:34:21 EDT 2003

"Phillip J. Eby" <pje at telecommunity.com> writes:

> At 10:15 AM 10/17/03 -0700, Guido van Rossum wrote:

>>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)

I like the look of this. In this context, it looks very natural.

> 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 means you're trying to be too clever, and should use parentheses

> 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):
>      ...

I'd rather not require parentheses in general. Guido's example of
sum(x for x in S) looks too nice for me to want to give it up without
a fight. But I'm happy to have cases where the syntax is ambiguous, or
even out-and-out unparseable, without the parentheses. Whether it's
possible to express this in a way that Python's grammar can deal with,
I don't know.

This signature intentionally left blank

More information about the Python-Dev mailing list