[Python-ideas] With clauses for generator expressions

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Nov 15 22:42:12 CET 2012

Phil Connell wrote:
> To me, 1 feels like it captures the semantics the best - the "with" clause is
> tacked onto the generator expression "(foo(line) ... for ... if)" and applies
> to the whole of that expression.

But that would break the existing rule that binding clauses
in a comprehension have to precede the expressions that use
the bound variable. If you're allowed to write

   (foo(line) for line in baz(f) with open('foo') as f)

then it's not obvious why you can't write

   (foo(line) if 'bar' in line for line in lines)

Are you suggesting that the latter should be allowed as


More information about the Python-ideas mailing list