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!
2 Answers 2
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.
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
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.
1 Comment
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.
__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 attributeGLOBAL = Global()at the module level and all of your other classes can reference it.GLOBAL = Global()is set. If that's the same module whereclass parentis defined, then some module that wants to inherit from parent would also useimport parentmodule; parentmodule.GLOBAL. In fact, you could putGLOBAL = Global()at the parent class level.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.self._var = 1suggests that different instances of these classes can get different configurations. IsGlobalto be considered a singleton?