PEP 289: Generator Expressions (please comment)

Alex Martelli aleax at aleax.it
Tue Oct 28 07:51:59 EST 2003


Skip Montanaro wrote:

> 
>     Alex> Let's see: today, I could code: sum([x*x for x in roots]) In
>     2.4, Alex> I will be able to code instead: sum(x*x for x in roots)
>     with some
>     Alex> performance benefits.  Could you please explain how using the
>     Alex> lighter-weight simple parentheses rather than today's somewhat
>     Alex> goofy ([ ... ]) bracketing "generally decreases readability"...?
> 
> How is using [...] to construct a list "somewhat goofy"?  For list

Juxtaposing round parentheses and square brackets because that's the
only way to feed a 'comprehension' to a function or type is "somewhat
goofy" in my opinion mainly because ([ and ]) look bad to me.  I'll be
tickled pink when I can omit those brackets (getting a small speed-up
by so doing will be just a little extra delight:-).

> comprehensions using square brackets to surround the expression make sense
> to me.  If they hadn't been done that way, you might be looking at using
> them to distinguish generator expressions from bracket-less list
> comprehensions. ;-)

When list comprehensions were the only way you could express such a
sequence inline, the brackets were understandably inevitable.  Now,
or to be more precise when 2.4 comes, I think I'll prefer to lose a
modicum of concision and write
    list(x*x for x in foo if x>bar)
rather than
    [x*x for x in foo if x>bar]
for reasons of uniformity that I have already explained in another
post.


Alex






More information about the Python-list mailing list