I have scraped some strings from emails into a list. The strings correspond to the names of functions which I want to be able to call later. I cannot call them in their current form so is there a way of converting the list of strings into a list of functions that I can call?
For example:
a = ['SU', 'BT', 'PL']
str = 'sdf sghf sdfgdf SU agffg BL asu'
matches = [x for x in a if x in str]
print(matches)
returns:
['SU', 'BL']
But I cannot call functions SU and BL from this list given the format.
your_dict[key](). Did you try that?import sysand thengetattr(sys.modules[__name__], FUNCTION_NAME). It will retrieve the variable for you from the global scope. For example, if you have already defined a function calledmyfunc, then you can dof = getattr(sys.modules[__name__], 'myfunc'). Then you can call the function from your variable:f(). It will call the functionmyfunc().stras a variable. Thank you>>> str(4) ; '4' ; >>> str = 'blalsd' ; >>> str(4) ; Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object is not callable