[Python-ideas] Is this PEP-able? "with" statement inside genexps / list comprehensions

Rhodri James rhodri at kynesim.co.uk
Tue Jul 31 07:32:00 EDT 2018


On 30/07/18 20:15, Rudy Matela wrote:
> Hello,
> 
> Do you think it would be nice to allow with statements inside genexps or
> list comprehensions?  The functions __enter__ and __exit__ would be
> automatically called as iterables are traversed.  I am thinking of
> drafting a PEP about this.  Examples:

I was worried until I twigged that you meant adding a clause rather than 
a statement per se.  How does this interact with the other clauses in a 
comprehension?  I'm thinking of monstrosities like:

g = (f.read() for fn in filenames if fn.endswith(".txt") with open(fn) 
as f for filenames in get_directory_listing(dir) if .....)

-- 
Rhodri James *-* Kynesim Ltd


More information about the Python-ideas mailing list