Assuming module foo
with method bar
:
import foo
method_to_call = getattr(foo, 'bar')
result = method_to_call()
You could shorten lines 2 and 3 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…
@akki Yes, if you're in the foo (the same) module you can use globals() to do this: methodToCall = globals()['bar'] --> This saved the dau
Top comments (0)