Consider these two snippets, both of which include class variable that references the prior class variable:
class A:
X = 1
Y = X+1
The first class parses and runs:
In [8]: a = A()
In [9]: a.Y
Out[9]: 2
Now consider a second class:
class TestDataSource:
import os
CONFIG_DIR = os.path.join(os.getcwd(),"configs")
FPATHS = [CONFIG_DIR + "schema{x}.yml" for x in range(1, 5)]
The second does not parse:
NameError Traceback (most recent call last)
Cell In[12], line 1
----> 1 class TestDataSource:
2 import os
4 CONFIG_DIR = os.path.join(os.getcwd(),"configs")
Cell In[12], line 5, in TestDataSource()
2 import os
4 CONFIG_DIR = os.path.join(os.getcwd(),"configs")
----> 5 FPATHS = [f"{CONFIG_DIR}/schema{x}.yml" for x in range(1, 5)]
Cell In[12], line 5, in <listcomp>(.0)
2 import os
4 CONFIG_DIR = os.path.join(os.getcwd(),"configs")
----> 5 FPATHS = [f"{CONFIG_DIR}/schema{x}.yml" for x in range(1, 5)]
NameError: name 'CONFIG_DIR' is not defined
I have tried variations on it including including the class Name :
FPATHS = [TestDataSource.CONFIG_DIR + "/schema{x}.yml" for x in range(1, 5)]
This gives:
NameError: name 'TestDataSource' is not defined
So why/how is the first class A
working and the second is not under any conditions I can come up with?