将Python函数作为参数传递而不执行它?
问题描述:
我有这样的功能:将Python函数作为参数传递而不执行它?
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)
的“如果一两”,从“一”功能的作品,但传递给“function_from_main”无は当“b”功能仍然执行从“a”检查它是否应该执行。
我该怎么办?
答
package2.b(do_this, do_that)
是一个函数调用(函数名称后跟括号)。相反,你应该只传递函数名称package2.b
功能a
您还需要修改功能a
使得函数被调用时
# 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})
+0
谢谢,这工作。 –
+0
如果解决方案正常工作,请将答案标记为已接受。这将帮助其他用户轻松找到答案。 – shanmuga
你叫'B'明确的条件时,如何它会*不*执行? – timgeb
@timgeb你完全理解我的问题。现在呢,我该如何让它不能执行? –
不叫它.. – timgeb