Why do I get this NameError in a generator within a Python class definition?
问题 In Python 3.5.0 this code: a = (1,2) class Foo(object): b = (3,4) c = tuple((i,j) for j in b for i in a) d = tuple((i,j) for i in a for j in b) produces: Traceback (most recent call last): File "genexprtest.py", line 2, in <module> class Foo(object): File "genexprtest.py", line 5, in Foo d = tuple((i,j) for i in a for j in b) File "genexprtest.py", line 5, in <genexpr> d = tuple((i,j) for i in a for j in b) NameError: name 'b' is not defined Why do I get this error? And why do I not get this