简体   繁体   中英

python high order function: generic functions calls with parameter list

I am looking for a high order function in python that takes in a function as parameter and a list of corresponding parameters, and call the function on the parameter list. Something like:

def exec(func,paramlist):
    return CALL(func,paramlist)  

The paramlist length is undetermined, since it goes with each func passed in. The CALL should be able to extract the elements in the list and put each parameter in the right slot and make the function call.

For reference, in Q language there is this "apply" function that handles generic function calls:

f1: {x}
f2: {x+y}
execFunction: {[fun;param] .[fun;param] }
execFunction[f1;enlist 1] // result is 1
execFunction[f2;(1 2)] // result is 3

如果func需要多个参数,并且paramlist是参数列表,则很简单:

func(*paramlist)

Python used to have an apply function but it was removed in Python 3 because it is so easy to unpack a list into individual arguments using the * operator. In fact an apply function can be written like so:

def apply(func, paramlist)
    return func(*paramlist)

This is so trivial, of course, that you wouldn't bother to write a function for it; you'd just write func(*paramlist) wherever you needed it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM