[Python-ideas] With clauses for generator expressions

Terry Reedy tjreedy at udel.edu
Thu Nov 15 18:56:47 CET 2012

On 11/15/2012 7:16 AM, Serhiy Storchaka wrote:
> On 15.11.12 13:11, Andrew Barnert wrote:
>> Which means the only question is, which one looks more readable:
>> 1. (foo(line) for line in baz(f) if 'bar' in line with open('foo') as f)
>> 2. (foo(line) for line in baz(f) with open('foo') as f if 'bar' in line)
>> 3. (foo(line) with open('foo') as f for line in baz(f) if 'bar' in line)
> What about such generator?
> def gen():
>      with a() as f:
>          for x in f:
>              if p(x):
>                  with b(x) as g:
>                      for y in g:
>                          if q(y):
>                              yield y

The yield expression is moved from last to first and the rest are left 
in order, as now.

Terry Jan Reedy

More information about the Python-ideas mailing list