I'm trying to see why this python code doesn't work:
class Foo:
T1 = (2, 3)
T2 = (5, 7)
L1 = [a for a in T1]
L2 = [b for b in T2]
L3 = [(c, d) for c in T1 for d in T2]
print(Foo.L1)
print(Foo.L2)
print(Foo.L3)
When I run this (in Python 3.7.7), I get this error:
Traceback (most recent call last):
File "./test.py", line 3, in <module>
class Foo:
File "./test.py", line 9, in Foo
L3 = [(c, d) for c in T1 for d in T2]
File "./test.py", line 9, in <listcomp>
L3 = [(c, d) for c in T1 for d in T2]
NameError: name 'T2' is not defined
Seems like there is something wrong with scoping? When I do the double list comprehension outside of the class definition, it works:
class Foo:
T1 = (2, 3)
T2 = (5, 7)
L1 = [a for a in T1]
L2 = [b for b in T2]
L3 = [(c, d) for c in Foo.T1 for d in Foo.T2]
print(Foo.L1)
print(Foo.L2)
print(L3)
Running the above:
[2, 3]
[5, 7]
[(2, 5), (2, 7), (3, 5), (3, 7)]
Is there something I'm doing wrong here?
BTW - I know this trivial example could be done with itertools.product, that's not the point of my question.