All other answers suggest to add some code outside the class SomeClass
definition. It may be ok in some cases, but in my case it was very inconvenient. I really wanted to keep the func_map
inside the class.
I suggest the following approach. Use not a class variable, but one more classmethod:
class SomeClass:
# ...
@classmethod
def get_func_map(cls):
return {'func1': cls.func1, 'func2': cls.func2}
@classmethod
def func3(cls, arg1):
# .....
cls.get_func_map()[func_name](arg1)
Of course you should modify this code so that a new dictionary not be constructed each time you call the get_func_map
method. It's easy, I did not do myself it to keep the example small and clear.
Tested on python 3.6