How to launch getattr function in python with additional parameters?
function to call
import sysdef wibble(a, b, foo='foo'): print(a, b, foo) def wibble_without_kwargs(a, b): print(a, b) def wibble_without_args(foo='foo'): print(foo) def wibble_without_any_args(): print('huhu')# have to be in the same scope as wibbledef call_function_by_name(function_name, args=None, kwargs=None): if args is None: args = list() if kwargs is None: kwargs = dict() getattr(sys.modules[__name__], function_name)(*args, **kwargs) call_function_by_name('wibble', args=['arg1', 'arg2'], kwargs={'foo': 'bar'})call_function_by_name('wibble_without_kwargs', args=['arg1', 'arg2'])call_function_by_name('wibble_without_args', kwargs={'foo': 'bar'})call_function_by_name('wibble_without_any_args')# output:# arg1 arg2 bar# arg1 arg2# bar# huhu