[Python-ideas] Assignments in list/generator expressions

Nick Coghlan ncoghlan at gmail.com
Tue Apr 12 07:06:33 CEST 2011

On Tue, Apr 12, 2011 at 1: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.

given/as resolves the parsing ambiguity problem by putting the names second:

ys = [y for x in xs given f(x) if p(x) else g(x) as y]

However, it does make it clear how limited such a clause still is if
it exists only at the comprehension level and isn't an expression in
its own right.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list