I wanted to define a class as follows:
class ConfigManager:
@classmethod
def load_config(cls):
# do some complex stuff to load something
return config
__CONFIG = ConfigManager.load_config()
@classmethod
def get_config(cls):
return cls.__CONFIG
And then when I run the following code, it reports a NameError:
x = ConfigManager.get_config()
Traceback (most recent call last):
File "test.py", line 1, in <module>
class ConfigManager:
File "test.py", line 7, in ConfigManager
__CONFIG = ConfigManager.load_config()
NameError: name 'ConfigManager' is not defined
Why does this error occur? Is it because Python code is executed by interpretation and when it goes to line 7, class ConfigManager is not yet finished to be defined?
@staticmethod- Olvin Roght@staticmethoddoes not work, since it still requiresclass ConfigManagerto be completely defined at first. - Trams