1

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?

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
  • 1
    list comprehensions create their own scope, a function scope, and class bodies do not create enclosing scopes (this is why you need to use the implicit reference `self` (the first positional argument to a method) to reference other methods!) – juanpa.arrivillaga Jul 24 '23 at 18:29
  • 1
    Note, if you just wrote the for-loop equivalent then it would work! Of course, you would then need to clean up the namespace manually, unless you don't mind it being polluted with `x`, for example – juanpa.arrivillaga Jul 24 '23 at 18:30
  • Anyway, another solution is simply to use `TestDataSource.FPATHS = [TestDataSource.CONFIG_DIR + "schema{x}.yml" for x in range(1, 5)]` after the class definitions. – juanpa.arrivillaga Jul 24 '23 at 18:32
  • 1
    @juanpa.arrivillaga Thanks for that insight! – WestCoastProjects Jul 24 '23 at 18:32
  • that last suggestion is problematic in that the logic has been orphaned from the enclosing class. stuff gets confusing and lost that way – WestCoastProjects Jul 24 '23 at 18:39

0 Answers0