How can I call the function John
inside function name_dispatcher
, by its name == str(B)
only and without using John
?
class A:
def name_dispatcher(self, person):
if person == "John":
self.John()
elif person == "Ben":
self.Ben()
def John(self):
print("I am john")
def Ben(self):
print("I am Ben")
A("John").C()
Because I want to aviod something in 'def name_dispatcher' when the number of parameters get largers, if I can call the function by its name, I don't need to add the additional if and elif condition as def name_dispatcher
.
Will the method better than if-else
? Or is a there any readable and efficient method to do so?