If I have multiple classes with a common interface such as:
class RobotA:
def do_smth(self):
return 1
class RobotB:
def do_smth(self):
return 2
class RobotC:
def do_smth(self):
return 3
I want to run the do_smth
method for each one (or for a sub-set of them) and get a list of the results.
Currently I do this with a simple function which takes an initialized class:
def do_smth(robots):
return [robot.do_smth() for robot in robots]
However this requires me to initialize each robot as a parameter in the function:
robots = [RobotA(), RobotB(), RobotC()]
do_smth(robots)
>> [1, 2, 3]
Q: Is this good design to do?
Also, problem is that if I have 20 such robots, the list becomes large.
Q: Is there a neat way (design pattern maybe) to make this code more easy to maintain / elegant?
Aucun commentaire:
Enregistrer un commentaire