List comprehension/genexp inconsistency.

J. Cliff Dyer jcd at sdf.lonestar.org
Tue Mar 20 16:23:22 EDT 2012


One of my coworkers just stumbled across an interesting issue.  I'm
hoping someone here can explain why it's happening.

When trying to create a class with a dual-loop generator expression in a
class definition, there is a strange scoping issue where the inner
variable is not found, (but the outer loop variable is found), while a
list comprehension has no problem finding both variables.

Demonstration:

>>> class Spam:
...     foo, bar = 4, 4
...     baz = dict(((x, y), x+y) for x in range(foo) for y in
range(bar))
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in Spam
  File "<stdin>", line 3, in <genexpr>
NameError: global name 'bar' is not defined
>>> class Eggs(object):
...     foo, bar = 4, 4
...     baz = dict([((x, y), x+y) for x in range(foo) for y in
range(bar)])
... 
>>> 

This was discovered in python 2.6.  In python 3.2, both versions fail
with the same NameError.  

Obviously, this is easy enough to work around.  I'm curious though:
What's going on under the hood to cause the nested generator expression
to fail while the list comprehension succeeds?

Cheers,
Cliff





More information about the Python-list mailing list