[Python-ideas] Assignments in list/generator expressions

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Apr 12 01:07:37 CEST 2011


Bruce Leban wrote:

>     ( for {seq} + 1 )   =>  ( x + 1 for x in seq )
>     ( for f({seq}) )    =>  ( f(x) for x in seq )
>     ( if {seq} > 1 }    =>  ( x for x in seq if x > 1 )

I don't think there's anything wrong whatsoever with the
current way of doing the first two of these.

The third one could perhaps benefit from something like

     (all x in seq if ...)

but I tend to agree that the gain is too small to be worth
a change.

> C# the lambda syntax is terse 
> and I find it unreadable if written without line breaks:
> 
>     f(x, y + 1, x => x + 1, y + 1)

Seems to me the readability problem there is mainly due to it
being mixed in with other comma-separated expressions. I'm not
sure it's really all that much better with lambda:

    f(x, y + 1, lambda x: x + 1, y + 1)

I'm not even sure offhand how that parses, so I'd probably put
parens around the lambda anyway to be sure:

    f(x, y + 1, (lambda x: x + 1), y + 1)

Now do that with the terse version:

    f(x, y + 1, (x => x + 1), y + 1)

This doesn't look significantly worse to me than any other
function call with operators in the argument expressions.

If there's a problem spotting the fact that there's an =>
in there, it could be made a little more noticeable:

    f(x, y + 1, (x ==> x + 1), y + 1)

-- 
Greg



More information about the Python-ideas mailing list