generator expression works in shell, NameError in script

greg greg at
Mon Jun 22 03:08:41 CEST 2009

guthrie wrote:

> -- I don't get this - the only local loop variable is "e", not lst.

Yes, but the whole list comprehension gets put into
a nested function, including the part that evaluates
lst. (It's not strictly *necessary* to do that, but
that's the way it happens to be implemented at the

> It would generally be the case that a nested function would have
> access to its enclosing scope.

Usually, but class namespaces are a special case --
they're not considered to be enclosing scopes, even
though textually they're written that way.

> it certainly appears un-intuitive

It is, but it's hard to see what could be done to
improve the situation without introducing worse


More information about the Python-list mailing list