[Python-ideas] Assignments in list/generator expressions

Greg Ewing greg.ewing at canterbury.ac.nz
Wed Apr 13 00:29:54 CEST 2011


Stephen J. Turnbull wrote:

> I don't have a problem with *reading* that, since you can't really
> win: "y" is used both before and after the "given" binding.

But the "before" usage is just the usual comprehension
quirk of putting the result expression before everything
else, even though it's actually evaluated last. Once you
allow for that, it's effectively after the given/letting/
whatever.

>  >    ys = [y for x in xs letting y = f(x) if y]
> 
> I'm sorry, but I read that three times and it parsed as "y gets
> undefined if it is false" every time.

In that case, would you prefer this?

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

-- 
Greg



More information about the Python-ideas mailing list