PHPs call_user_func_array in Python
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)
精彩评论