将带参数的函数传递给 Python 中的另一个函数? [英] Passing functions with arguments to another function in Python?

查看:17
本文介绍了将带参数的函数传递给 Python 中的另一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将带参数的函数传递给 Python 中的另一个函数?

Is it possible to pass functions with arguments to another function in Python?

说出以下内容:

def perform(function):
    return function()

但是要传递的函数将具有如下参数:

But the functions to be passed will have arguments like:

action1()
action2(p)
action3(p,r)

推荐答案

你是这个意思吗?

def perform(fun, *args):
    fun(*args)

def action1(args):
    # something

def action2(args):
    # something

perform(action1)
perform(action2, p)
perform(action3, p, r)

这篇关于将带参数的函数传递给 Python 中的另一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆