I would normally write a simple class like the one below. Initialize a python list in __init__
and collect results from different methods. This works as expected.
class my_class:
def __init__(self, res = []):
self.res = res
def method_1(self):
self.res.append('a')
def method_2(self):
self.res.append('b')
obj = my_class()
obj.method_1()
obj.res
obj.method_2()
obj.res
However, the number of methods grows very quickly and I was hoping to break it to smaller classes. I am not sure if this can be done with a class inheritance, see below. My goal here is to have a global self.res
that records results from calls of various methods and can be accessed across smaller classes.
class my_class:
def __init__(self, res = []):
self.res = res
class my_class_1(my_calss):
def __init__(self, res):
super().__init__(res)
def method_1(self):
self.res.append('a')
class my_class_2(my_class):
def __init__(self, res):
super().__init__(res)
def method_2(self):
self.res.append('b')
[Update]: The context for the question: I am building a webpage which contains different tabs; for each tab it contains figures and tables. Instead of using one class, which grows lines quickly, I decide to create separate classes for each tab. However, I want to maintain a dictionary that collects all the figures and tables across the classes, so that I can apply logic to update figure from the trigger from another table (table and figure could be at different tabs). I am not sure what is the best class structure.