在Python中,如何将一个函数(回调)作为另一个函数的参数使用?

137
假设我有一些代码,如下所示:
def myfunc(anotherfunc, extraArgs):
    # somehow call `anotherfunc` here, passing it the `extraArgs`
    pass

我想将另一个现有的函数作为anotherfunc参数传递,并将参数列表或元组作为extraArgs,并让myfunc使用这些参数调用传入的函数。

这种做法可行吗?我该如何实现?


相关链接:https://dev59.com/gqfja4cB1Zd3GeqPpxHH#47634215 - alseether
11个回答

0

这里是一个例子:

def x(a):
    print(a)
    return a

def y(func_to_run, a):
    return func_to_run(a)

y(x, 1)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接