How can I associate dictionary keys with functions?
dictionary, python
Solution
myDict["Objective A"] = MyClass.FuncA()
associates the return value of `MyClass.FuncA`. If you want to associate the function itself:
myDict["Objective A"] = MyClass.FuncA
myDict["Objective B"] = MyClass.FuncB
Then you can call it directly:
myDict["Objective A"]()
Problem
My application has a class full of functions that perform various operations. These operations are related to 1 and only 1 dictionary key. How can I associate the dictionary key with its respective function? The goal of the tool will be for it to use the appropriate set of functions when given a set of keys. This is how I currently work it out: ``` class MyClass: def FuncA(self): # Some code ----------------------------- def FuncB(self): # Some code ----------------------------- myDict["Objective A"] = MyClass.FuncA() myDict["Objective B"] = MyClass.FuncB() ``` Due to the nature of my program, the set of keys that will be used can change from time to time. Therefore, hard coding it as I have makes no sense. I want to be able to loop through my set of keys and run the appropriate functions. Remember, ObjectiveA is associated with FuncA. Therefore, if ever ObjectiveA was to appear in my list of keys, FuncA would need to be called in order to process some operation and populate the respective value pair. Envisioning this: ``` for k in myDict.keys(): myDict[k] = MyClass.TheAssociatedFunction() ```