2017-04-22 213 views
-2

我有這樣的功能:將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」檢查它是否應該執行。

我該怎麼辦?

+1

你叫'B'明確的條件時,如何它會*不*執行? – timgeb

+0

@timgeb你完全理解我的問題。現在呢,我該如何讓它不能執行? –

+0

不叫它.. – timgeb

回答

1

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

相關問題