如何在不执行函数的情况下将函数作为参数传递?
How to pass function as argument without executing it?
我有这个功能:
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})
我有这个功能:
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})