[Python-ideas] Assignments in list/generator expressions
Nick Coghlan
ncoghlan at gmail.com
Tue Apr 12 01:49:12 CEST 2011
On Tue, Apr 12, 2011 at 9:17 AM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> 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).
This one is tricky, since the assignment is *after* the for loop, but
*before* the filter condition.
You could reorder it as below, but the translation to long-form Python
code wouldn't be quite as clean (since the order of clauses wouldn't
follow the order of nesting any more).
ys = [y for x in xs if y given f(x) as y]
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas
mailing list