如何在Python中使用函數名稱作爲參數? 例如:如何傳遞函數名稱列表作爲參數?
def do_something_with(func_name, arg):
return func_name(arg)
其中 'FUNC_NAME' 可能是 '平均', '性病', '排序' 等
例如:
import numpy as np
func_list = ['mean', 'std']
for func in func_list:
x = np.random.random(10)
print do_something_with(func, x)
當然結果和應該在我的數組'x'上成功應用'mean'和'std'。
改爲傳遞函數列表。 'func_list = [np.mean,np.std]' –
[Python-將函數傳遞給另一個函數]可能的重複(http://stackoverflow.com/questions/1349332/python-passing-a-function-into-另一種功能)(儘管大部分的實際問題是無關緊要的,根本問題和答案是有效的) –