[Python-3000] Is this a bug with list comprehensions or not?

Guido van Rossum guido at python.org
Fri Jul 11 15:03:37 CEST 2008


On Fri, Jul 11, 2008 at 12:00 AM, Raymond Hettinger <python at rcn.com> wrote:
> From: "Guido van Rossum" <guido at python.org>
>>
>> The new failure in 3.0 is a side effect of the translation (mostly) of
>> list comps into genexps. The underlying problem is that the genexp
>> creates a function scope, and function scopes don't have access to the
>> surrounding class body scope. (There's a really good but extremely
>> subtle reason for that.)
>
> Would you be open to an alternate implementation for list/set/dict comps
> that doesn't use a genexps?  That would solve the scope problem, the
> performance hit, and make tracing more straight-forward.

Well, the problem is that we specifically decided that the difference
between listcomps and genexps is a source of problems and confusion,
and that consequently a listcomp [<genexp>] should be implemented as
completely equivalent to list(<genexp>).

I'd be happy to see a solution that implements genexps more
efficiently without changes in semantics (and then listcomps would
also benefit).

> If you're open to it, I can see if the EuroPython sprinters can find an
> implementation closer to what we had before but using the compiler to create
> an invisible induction variable.
>
> Not sure if the current implementation was really a winning trade. It may
> have been better to have the minor but easy to explain nuisance
> of exposing the induction variable instead of having the harder-to-explain
> subtleties associated with a slower underlying genexp.

No, based on feedback over the years the exposure of the induction
variable is a real problem that needs to be rooted out.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list