0

I am looking to create a run configuration class called Global that will store important variables. This class will be passed to many classes and their children. For example:

class Global:
 def __init__(self):
 self._var = 1
 @property
 def var(self):
 return self._var
class parent:
 def __init__(self, GLOBAL):
 self.GLOBAL = GLOBAL
 print(self.GLOBAL.var) # this works
class child(parent):
 def __init__(self):
 print(self.GLOBAL.var) # this doesn't work (because referencing the parent's instance variable)

How can I make the Global class accessible to all children of the parent class (without passing it to each child's constructor)? I also tried creating a class variable in the parent class and assigned it's value when the class is initialized. However, that didn't work.

Also, if there is a better way to store mutable run configurations and pass them to many classes and their children I'd be happy to change my strategy.

Thanks a ton in advance!

asked May 12, 2020 at 22:31
8
  • 1
    Your __init__ Nevers sets the attribute in your subclass and it overrides the superclass one that does... This really has nothing to do with your global class at all, this would work the same with any attribute Commented May 12, 2020 at 22:40
  • 1
    Why use class inheritance at all? Just do GLOBAL = Global() at the module level and all of your other classes can reference it. Commented May 12, 2020 at 23:06
  • 2
    No, it would only be in the module where GLOBAL = Global() is set. If that's the same module where class parent is defined, then some module that wants to inherit from parent would also use import parentmodule; parentmodule.GLOBAL. In fact, you could put GLOBAL = Global() at the parent class level. Commented May 12, 2020 at 23:20
  • 1
    There is the question of when Global() should be instantiated - if its __init__ is reading configuration, that may be a bad thing. But if its used before the real configuration happens, that's a bad thing tool. That's the advantage of it just being at the module level. Your code can initialize it after everything's been imported but before the classes have been instantiate it. Commented May 12, 2020 at 23:22
  • 1
    self._var = 1 suggests that different instances of these classes can get different configurations. Is Global to be considered a singleton? Commented May 12, 2020 at 23:26

2 Answers 2

2
  1. Your child class should call parent __init__ method like this:

    super().__init__(...)

Otherwise you won't set GLOBAL field at all. I guess you can read more here.

  1. You want to create a global object, don't you? I mean, not a class, but the object of that class, because you're accessing variable var in your constructor. Anyway, you can just create a variable in your module scope and use it.

    Global: ...

    OtherClass: def run(self): print(_global_settings)

    _global_settings = Global() # or something similar

  2. You'd better follow PEP8 style if possible, then your code will be easier to read and understand. In particular, capitalize your class names and use lowercases for class fields.

answered May 12, 2020 at 22:57
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @stardust thanks for the response. The global object needs to be referenced across modules, but that would have worked well. And noted on the style.
1

If your child class does not override the constructor __init__, then parent.__init__ will be used when constructing child objects, and you will get the functionality you want. If you need to write specialized constructors to your derived classes, however, you will need to pass the Global object to an explicit call of the parent.__init__ constructor within child.__init__.

Python does not have automatic constructor chaining.

answered May 12, 2020 at 22:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.