开发者

PHPs call_user_func_array in Python

开发者 https://www.devze.com 2022-12-11 05:36 出处:网络
Is there an equivalent in Python for PH开发者_StackOverflow社区P\'s call_user_func_array?Call the function with the array preceded by *:

Is there an equivalent in Python for PH开发者_StackOverflow社区P's call_user_func_array?


Call the function with the array preceded by *:

function(*array)


if your function name is a string, you could do:

getattr(obj, 'func')(*arr)     # where obj is the namespace that hold func


Your can call function and pass arguments by * sign
ex.

def add(a, b):
    return a + b

arg = (1, 2)
add(*arg)

You also can use dict to pass argument pairs by double star **

ex.

arg = {a: 1, b: 2}
add(**arg)
0

精彩评论

暂无评论...
验证码 换一张
取 消