[Python-ideas] With clauses for generator expressions

Greg Ewing greg.ewing at canterbury.ac.nz
Sat Nov 17 00:28:34 CET 2012


Nick Coghlan wrote:
> That is, the outermost iterable is evaluated in the *current* scope, not 
> inside the generator.

I've always felt it was a bad idea to bake this kludge into
the language. It sweeps a certain class of problems under the
rug, but only in *some* cases. For example, in

    ((x, y) for x in foo for y in blarg)

rebinding of foo is guarded against, but not blarg. And if
that's not arbitrary enough, in the otherwise completely
equivalent

    ((x, y) for y in blarg for x in foo)

it's the other way around.

Anyhow, it wouldn't be *impossible* to incorporate a with-clause
into this scheme. Given

    (upper(line) with open(name) as f for line in f)

you either pick open(name) to be the pre-evaluated expression,
or not do any pre-evaluation at all in that case. Either way,
it can't break any *existing* code, because nobody is writing
genexps containing with-clauses yet.

-- 
Greg



More information about the Python-ideas mailing list