[Python-ideas] Assignments in list/generator expressions

Eugene Toder eltoder at gmail.com
Mon Apr 11 02:18:35 CEST 2011


On Sun, Apr 10, 2011 at 7:06 PM, Guido van Rossum <guido at python.org> wrote:
> Can you please be so kind to leave attributions in? (I'm glad you snip stuff
> you don't need, but the attributions are essential.)
>
<snip>
>
> It was not clear.

Will try to improve on both accounts.

On Sun, Apr 10, 2011 at 7:06 PM, Guido van Rossum <guido at python.org> wrote:
> (That's not to say that I am vetoing a specific proposal. But the bar
> is high, and inventing a "Pythonic" syntax is a prerequisite. So is a
> good story with a set of examples explaining why the new feature adds
> something you cannot easily do today.)

This particular feature is not the top on my wish list either. I just
don't see a good explanation for why it's missing. It's a lesser-known
feature in Haskell (no mention in Wikipedia! :-), but it is handy at
times. As mentioned above, people suggest it for Python from time to
time.

As for the syntax, there are many choices. Going with <keyword> name =
expr, <keyword> can be 'let', as in Haskell, F# and Clojure, or
'where' as Greg suggested above, or 'given' as it's related to PEP
3150 -- the scope of name partially extends to the left of it's
definition. From the backward compatibility point, 'let' is likely too
short for a new keyword, and PEP 3150 describes the problem with
'where'.

ys = [y for x in xs where y = f(x) if y]

ys = [y for x in xs given y = f(x) if y]

There are likely other words that will do. Alternatively, some
punctuation can be used, e.g. semicolon:

ys = [y for x in xs; y = f(x) if y]

Eugene



More information about the Python-ideas mailing list