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!