1

I'd like to initialize two class variables: the first one is a constant, the second one a list (or set, dict, etc) that is built based on the value of the first constant. An example of what I'd like to do, which doesn't work:

class ThisDoesntWork:
   SOME_CONST = "hello"
   SOME_OTHER_CONST = list(SOME_CONST + str(i) for i in range(4))

Results in:

NameError: name 'SOME_CONST' is not defined

As found in another answer, the following code works, as long as there is no list, dict, set:

class ThisWorks:
   SOME_CONST = "hello"
   SOME_OTHER_CONST = SOME_CONST + str(1)

It feels like I'm missing some core concept in how classes and namespaces work, which breaks the first example but not the second. What is it, and what is a good way to do this well?

0 Answers0