I have a class with many member functions, and I want self.func to have function pointer based on provided name. However, this way each class should have a dict from all names to functions which is clearly a waste. Is there a better way to do it? The issue with using class variable (instead of member variable) is that some of those functions depend on the information from the instance.
class C:
def __init__(func_name):
name_to_func = {"f1": self.func1, "f100": self.func2}
self.func = name_to_func[func_name]
def func1(self): return 1
def func100(self): return self.a
Using following method needs a different usage (c = C("f1"); c.func(c)) instead of (c = C("f1"); c.func()):
class C:
name_to_func = {"f1": C.func1, "f100": C.func2}
def __init__(func_name):
self.func = name_to_func[func_name]