我有这个功能:
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/