I have a Python class called FunctionsManager; its __init__() method is the following:
class FunctionsManager:
def __init__(self, instance_class1, instance_class2, ..., instance_classN):
self.__instance_class1 = instance_class1
self.__instance_class2 = instance_class2
...
self.__instance_classN = instance_classN
At the moment the value of N is 8, but with this architecture its value increases every time I have to develop a new specification.
The reason why in the class FunctionsManager there are many attributes initialized by the __init__() method it is that in the class there are 2 methods with the following structure:
class FunctionsManager:
...
# this method manages the data received from a caller, and depending
# on the value received it uses one of the instance attribute
def handle_received_data(self, data):
if data[0] == 1:
result = self.__instance_class1.handle(data)
elif data[0] == 2:
result = self.__instance_class2.handle(data)
...
elif data[0] == N:
result = self.__instance_classN.handle(data)
return result
# this method by the attributes instance check if there new data to send and
# prepares this data to return them to the caller; it uses one of the instance attribute
def prepare_data(self):
if self.__instance_class1.data_ready():
data = self.__instance_class1.get_data()
elif self.__instance_class2.data_ready():
data = self.__instance_class2.get_data()
...
elif self.__instance_classN.data_ready():
data = self.__instance_classN.get_data()
return data
In my opinion the structure of this part of the project does not conform to correct object-oriented programming, but I don't know how to change it.
To be more precise my goal is to modify the project to reduce the number of instances used to create an instance of the class FunctionsManager.
prepare_data()runs over those instances, in order, and checksdata_ready(). I assume there is some other concurrent "process" that prepares the data? Because if that call is blocking, then it will always returns the first instance. If it is not, then there is no fallback (what if none of them have data ready?). More importantly, it sounds like you start concurrent tasks, and then gather results? If so, then I would strongly advice redesigning this. Perhaps by using async with gather.FunctionsManagerto adataclass. I have read the dataclasses documentation. I didn't know it.