In a python project I've been using static members of nested classes to store static configuration (e.g. table names for external data-stores and validation functions for data structures)
class a(object):
foo = 1
bar = 2
class b(object):
spam = "spam"
class c(object):
eggs = "eggs"
This allows me to quickly and easily get a config lookup, which my IDE (pycharm) can type/existence check and suggest auto-completions as I write code.
>>> a.b.spam
'spam'
>>> a.c.eggs
'eggs'
Suppose now, I want to add another nested class which refers to static members of a:
class a(object):
foo = 1
bar = 2
class b(object):
spam = "spam"
class c(object):
eggs = "eggs"
class d(object):
spams = "spam" * a.bar
spam_and_eggs = a.b.spam + a.c.eggs
In a python console, this works as I'd like:
>>> a.d.spams
'spamspam'
>>> a.d.spam_and_eggs
'spameggs'
But putting the above into a .py file and running it as a module, I get name-errors:
Traceback (most recent call last):
File "/usr/lib64/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib64/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/me/example/test.py", line 1, in <module>
class a(object):
File "/home/me/example/test.py", line 11, in a
class d(object):
File "/home/me/example/test.py", line 12, in d
spams = "spam" * a.bar
NameError: name 'a' is not defined
Is there a combination of imports or a way I could solve this name-error? Alternatively is there a more pythonic way to implement this style of configuration which is free from this issue?