0

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.

Alpha123
  • 37
  • 8
  • 2
    `cls.__dict__` only contains the attributes in the current class, not inherited attributes. See https://stackoverflow.com/questions/44028984/list-all-attributes-which-are-inherited-by-a-class for how to get inherited attributes. – Barmar Aug 21 '23 at 19:47
  • 1
    "my aim was to use the class directly and not to initialise it for this use case." then you probably shouldn't be using a class, but anyway, `__dict__` is the actual namespace of the object, in this case, the class. You could go through the MRO and retreive the other attributes from the other classes – juanpa.arrivillaga Aug 21 '23 at 19:58
  • @Barmar jeez all of those answers are not very good – juanpa.arrivillaga Aug 21 '23 at 20:07
  • Hi @juanpa.arrivillaga thank you for your response, in such a scenario where I have some common settings for all use cases (setup, config, delete) what would be the better approach if not class inheritance - just wanted to know how best to deal this with – Alpha123 Aug 21 '23 at 20:08
  • Your goal is hard to understand. What do you mean "values need to be fetched via different API calls dynamically?". If you want to access attributes dynamically, use the built-in `getattr()`. – Barmar Aug 21 '23 at 20:10
  • @Barmar What I wanted to convey is that the dict I'm trying to prepare has dynamic values so I can not hardcode - maybe I will edit the first line and remove the dynamic part to avoid confusion – Alpha123 Aug 21 '23 at 20:15
  • `getattr()` is dynamic. Why do you need a dictionary? – Barmar Aug 21 '23 at 20:19
  • @Barmar My use case is that I need to construct a dictionary of settings and the values of these dict are dynamic and this has nothing to do with my question - sorry again for the confusion – Alpha123 Aug 22 '23 at 08:35
  • Why not just use `CommonSettings.a` instead of doing this whole inheritance thing? What does being able to refer to it as `SetupSettings.a` get you? – Samwise Aug 22 '23 at 16:47

0 Answers0