Is there a way to convert a list of function or definition names (strings) into a dictionary based lookup table where the key is the definition name and the value is the definition class that corresponds to the name (the definition name is the name given). It has been said that the safest way is to use the lookup table approach (dictionary mapping) such as here. However, the situation is such that the list of definition names is variable and the user/programmer is not able to directly write it into the code but rather it has to be added programmatically.
So how can I do effective the following:
nameList = ['methodName1','methodName2','methodName3','methodName4']
methodLookup = {}
for name in nameList:
methodLookup.update({name:name.strip('\'')})
Where the dictionary value is the function not the string.
Something similar in essence to but the opposite of the following:
for var, val in dictionary.items(): exec(var + ' = val')
I guess you could use something like this:
def plus1(x):
return x + 1
def plus2(x):
return x + 2
function_list = ['plus1', 'plus2']
function_dict = {function_name: globals()[function_name] for function_name in function_list}
And then after that you can call plus1
function by:
function_dict['plus1'](5)
#6