I am trying to create a class with multiple constants (in Python 3.6). Because the constants are lists and dictionaries with a lot of correlated values I want to generate some constants using the others.
While trying to do so I stumbled upon some limitations which I don't fully understand. I will show this in the following examples:
Class Example1
is created without errors:
class Example1: # this works
C1 = [1, 2, 3]
C2 = [2, 3, 4]
C3 = [C2, C2]
C4 = [C1[0], C2[2]]
C5 = [str(c1) for c1 in C1]
C6 = [c1 + c2 for c1, c2 in zip(C1, C2)]
However trying to create Example2
:
class Example2: # this doesn't work
C1 = 1
C2 = [1, 2, 3]
C3 = [C1 + c2 for c2 in C2]
raises NameError: name 'C1' is not defined
while trying to create C3
.
The same happens with Example3
:
class Example3: # this doesn't work
C1 = [1, 2, 3]
C2 = [2, 3, 4]
C3 = [c1 + c2 for c2 in C2 for c1 in C1]
Can someone provide an explanation why Example2
and Example3
don't work but Example1
does? What are the rules here? Is there any alternative way to initialize constant C3
in Example2
and Example3
inside the class?
P.S. Sorry for asking multiple subquestions.