Question
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, and I have a string whose contents are "bar". What is the best way to go about calling foo.bar()? I need to get the return value of the function, which is why I don't just use eval. I figured out how to do it by using eval to define a temp function that returns the result of that function call, but I'm hoping that there is a more elegant way to do this.
How-To
Assuming module foo with method bar:
- import foo
- method_to_call = getattr(foo, 'bar')
- result = method_to_call()
- result = getattr(foo, 'bar')()
沒有留言:
張貼留言