python - 如何将函数作为参数传递而不执行它?

标签 python function keyword-argument

我有这个功能:

def a(one, two, the_argument_function):
    if one in two:
        return the_argument_function

我的 the_argument_function 看起来像这样:

def b(do_this, do_that):
    print "hi."

以上两者都导入到文件“main_functions.py”中,我的最终代码如下所示:

print function_from_main(package1.a, argument, package2.b(do_this, do_that)

“a”函数中的“if one in Two”可以工作,但“b”函数在传递给“function_from_main”时仍然会执行,而无需等待“a”的检查来查看它是否确实应该执行。

我能做什么?

最佳答案

package2.b(do_this, do_that) 是一个函数调用(函数名称后跟括号)。相反,您应该仅传递函数名称 package2.b 函数 a

您还需要修改函数a,以便在满足条件时调用函数be

# function a definition 
def a(one, two, the_argument_function, argument_dict):
    if one in two:
        return the_argument_function(**argument_dict)

def b(do_this, do_that):
    print "hi."

# function call for a
a(one, two, b, {'do_this': some_value, 'do_that': some_other_value}) 

关于python - 如何将函数作为参数传递而不执行它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43558006/

相关文章:

javascript - 关于 "var avg = array.average()"中 array.average() 功能的混淆

Python 3 kwargs 洞察

python - Sklearn 将 fit() 参数传递给管道中的 xgboost

python - (Python)得到最大的递归错误,在这种情况下我应该尝试其他方法还是重写它?

python - 打印组合字符串和数字

python - 从二进制依赖文件导入错误

python - 查找按钮的 xpath,在 python 和 selenium 中使用它

function - scala隐式类方法类型不匹配在reduce与函数currying的非隐式方法中

c - 什么时候可以使用 "void()",这样做有什么好处

python - Amazon lambda dynamodb update_item() 只接受关键字参数