2017-06-05 21 views
1
def test(): 
    print "printing from test func" 

dic = {'a':1,'b':2,'c':test} 
if len(dic) == 3: 
    print (dic['c']) 

output: 
<function test at 0x106887578> 

此代碼將函數對象作爲輸出打印,如何從測試函數獲取打印語句作爲輸出。當存儲爲字典值時調用函數

+0

你試過調用函數嗎?你可能想要返回的功能,而不是打印。 – Artyer

+0

您需要使用[調用語法](https://docs.python.org/3/reference/expressions.html#calls)。即'F()' –

回答

10

只需撥打dic['c']()即可運行您的功能。希望我幫助!

相關問題