-
Notifications
You must be signed in to change notification settings - Fork 181
Open
Description
class Test:
a = [1, 2, 3]
b = [1, 2, 3]
c = [i*j for i in a for j in b]Pyflakes gives no errors here. But believe it or not, this raises a NameError
Traceback (most recent call last):
File "test.py", line 1, in <module>
class Test:
File "test.py", line 4, in Test
c = [i*j for i in a for j in b]
File "test.py", line 4, in <listcomp>
c = [i*j for i in a for j in b]
NameError: name 'b' is not defined
The reason has to do with how Python treats the outermost comprehension iterable differently. See https://stackoverflow.com/questions/13905741/accessing-class-variables-from-a-list-comprehension-in-the-class-definition. Note that the following does not give any errors when executed
class Test:
a = [1, 2, 3]
b = [1, 2, 3]
c = [i for i in a]Metadata
Metadata
Assignees
Labels
No labels