How to launch getattr function in python with additional parameters?

前端 未结 3 1831
栀梦
栀梦 2021-01-31 09:08

I want to call some unknown function with adding parameters using getattr function. Is it possible?

3条回答
  •  情话喂你
    2021-01-31 09:29

    If you wish to invoke a dynamic method with a dynamic list of arguments / keyword arguments, you can do the following:

    function_name = 'wibble'
    args = ['flip', 'do']
    kwargs = {'foo':'bar'}
    
    getattr(obj, function_name)(*args, **kwargs)
    

提交回复
热议问题