[Python-ideas] With clauses for generator expressions

Andrew Barnert abarnert at yahoo.com
Thu Nov 15 16:32:06 CET 2012


> From: Serhiy Storchaka <storchaka at gmail.com>
> Sent: Thu, November 15, 2012 6:42:35 AM
> 
> And what about this (only one for/if for  simplicity)?
> 
>     with a() as f:
>          for x in f:
>             with b() as  g:
>                 if p(x):
>                      with c() as  h:
>                          yield  x


If you can only have one with per for, this doesn't have a direct translation.

However, if you want to extend it to have any number of withs per for, that 
seems to rule out option 2, and maybe option 1, but seems fine with option 3:

(x with a() as f for x in f with b() as g if p(x) with c() as h)

The fact that option 3 can obviously do something which seems impossible in 
option 2, and which I can't work out in a few seconds off the top of my head 
with option 1, may be a more compelling argument than the fact that option 1 
instinctively looked cleaner to me (and the one other person who commented on 
the three choices).




More information about the Python-ideas mailing list