What is the best way to go about calling a function given a string with the function's name in a Python program. For example, let's say that I have a module
foo with method
import foo methodToCall = getattr(foo, 'bar') result = methodToCall()
As far as that goes, lines 2 and 3 can be compressed to:
result = getattr(foo, 'bar')()
if that makes more sense for your use case. You can use
getattr in this fashion on class instance bound methods, module-level methods, class methods... the list goes on.