J
J. Cliff Dyer
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:
.... 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
.... 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
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:
.... 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
.... 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