[Python-ideas] Assignments in list/generator expressions

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


Nick Coghlan wrote:

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

Hmmm. Here I find myself getting tripped up by the ordering
when I try to read that smoothly.

I think it's because words like 'given' or 'where', as used
in mathematics, imply a definition of something that's been
used *before*, whereas here it's defining something to be
used *after* (i.e. in the following 'if' clause).

Mathematicians use 'let' when they're defining something to
be used subsequently. So it should really be either

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

or

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

Incidentally, I think this also demonstrates that proposing
a new syntax with a <new-keyword-goes-here> placeholder doesn't
really work -- usually the choice of keyword matters, and it
has an effect on how the rest of the syntax should be arranged.
Or at least it does to someone with a strong sense of language
design aesthetics.

-- 
Greg




More information about the Python-ideas mailing list