[Python-ideas] With clauses for generator expressions

Steven D'Aprano steve at pearwood.info
Fri Nov 16 07:05:02 CET 2012

On 15/11/12 22: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)

Is that a trick question?

Answer: None of them.

In my opinion, they are all too busy for a generator expression and should
be re-written as a generator function.

As far as the given use-case is concerned:

upperlines = (line.upper() for line in open('foo'))

I don't see what the concern is. The file will remain open so long as the
generator is not exhausted, but that has to be the case no matter what you
do. If the generator is thrown away before being exhausted, the file will
eventually be closed by the garbage collector, if only when the application
or script exits. For short-lived scripts, the temporarily leakage of a file
handle or two is hardly likely to be a serious problem.

Presumably if you have a long-lived application with many such opened
files, you might risk running out of file handles when running under Jython
or IronPython. But I think that's a sufficiently unusual and advanced use-
case that I'm not worried that this is a problem that needs solving with
syntax instead of education.


More information about the Python-ideas mailing list