[Python-ideas] Assignments in list/generator expressions

Eugene Toder eltoder at gmail.com
Tue Apr 12 14:10:55 CEST 2011


On Mon, Apr 11, 2011 at 11:53 PM, Carl M. Johnson
<cmjohnson.mailinglist at gmail.com> wrote:
> On Mon, Apr 11, 2011 at 2:14 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>
>>  ys = [y for x in xs letting y = f(x) if y]
>
> Isn't that ambiguous? What if someone used the conditional expression:
> ys = [y for x in xs letting y = f(x) if y else g(x)] ? It seems like
> the letting/given has to go last in order to eliminate the possible
> ambiguity in the subsequent optional if clause.

This ambiguity already exists -- you can write conditional expression
in iteratee of for or in condition of if, but it won't parse, because
the conflict is resolved in favour of list comprehension's 'if'. You
have to put if/else in parenthesis.

Eugene



More information about the Python-ideas mailing list