Note: A similar question already exists, albeit being for C#.
Let's suppose my code looks like the following:
class SomeClass:
def __init__(self):
pass
def do(self):
print("a")
class SomeOtherClass:
def __init__(self):
pass
def do(self):
print("b")
class B:
def __init__(self):
self.SomeClass = SomeClass()
self.SomeOtherClass = SomeOtherClass()
def __main__():
myclass = B()
desired_class = str(input("The method of which class do you want to execute? "))
myclass.desired_class.do()
I will not know at built-time what method of SomeClass
will be called. If-then-else is also not very neat if you have 200 methods and no just 2 to choose from.
How is this done most efficiently in python?
Note: The method
will always be an existing method of SomeClass
.