相关疑难解决方法(0)

Python函数作为函数参数?

Python函数可以作为另一个函数的参数吗?

说:

def myfunc(anotherfunc, extraArgs):
    # run anotherfunc and also pass the values from extraArgs to it
    pass
Run Code Online (Sandbox Code Playgroud)

所以这基本上是两个问题:

  1. 它是否允许?
  2. 如果是,如何在其他功能中使用该功能?我需要使用exec(),eval()或类似的东西吗?永远不需要弄乱它们.

BTW,extraArgs是anotherfunc参数的列表/元组.

python arguments function

103
推荐指数
4
解决办法
18万
查看次数

如何将函数名称列表作为参数传递?

如何在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'.

python function

2
推荐指数
2
解决办法
3016
查看次数

标签 统计

function ×2

python ×2

arguments ×1