Python函数可以作为另一个函数的参数吗?
说:
def myfunc(anotherfunc, extraArgs):
# run anotherfunc and also pass the values from extraArgs to it
pass
Run Code Online (Sandbox Code Playgroud)
所以这基本上是两个问题:
BTW,extraArgs是anotherfunc参数的列表/元组.
如何在Python中使用函数名作为参数?例如:
def do_something_with(func_name, arg):
return func_name(arg)
Run Code Online (Sandbox Code Playgroud)
其中'func_name'可能是'mean','std','sort'等.
例如:
import numpy as np
func_list = ['mean', 'std']
for func in func_list:
x = np.random.random(10)
print do_something_with(func, x)
Run Code Online (Sandbox Code Playgroud)
当然,结果应该在我的数组'x'上成功应用'mean'和'std'.