I have a requirement where I need to construct and use a dictionary based on the use case. So I came up with below implementation
config.py
class CommonSettings:
a='a'
b='b'
c, d = function_to_get_c_and_d_values()
@classmethod
def to_dict(cls):
return {k:v for k, v in cls.__dict__.items() if not ((k.startswith('__')
and k.endswith('__'))
or k == 'to_dict')}
class SetupSettings(CommonSettings):
e='e'
f='f'
f1=get_f1()
class TeardownSettings(CommonSettings):
g='g'
h='h'
h1=get_h1()
main.py
from config import SetupSettings, TeardownSettings
#Do Stuff
setup_settings = SetupSettings.to_dict() # Not working
a = SetupSetting.a # Not working
#Do more stuff
teardown_settings = TeardownSettings.to_dict(). # Not working
#Do some more stuff
with the above implementation, the child classes are not able to access any variables or functions of the parent class. How do I fix this? Firstly is it possible to access parent class variables and functions without an init method?
Note: I wantedly skipped using init methods in the classes as my aim was to use the class directly and not to initialise it for this use case.