My program needs to select a class based on user input.
The classes have the same set of methods (inherited from the same ABC) with different implementations. And there are probably more than 30 classes.
# myABC.py
class myABC(ABC):
@abstractmethod
def foo():
pass
# A.py
class A(myABC.myABC):
def foo():
print("A")
# B.py
class B(myABC.myABC):
def foo():
print("B")
I have only one way in my mind is using if-else. But that is obviously not a great option with more than 30 if, elif statements
# main.py
def get_obj(usr_input: str):
if usr_input == "A":
return A.A()
elif usr_input == "B":
return B.B()
...
Are there any other ways that can solve this problem? Thanks a lot.