我已經包含了一堆的功能和它們的鍵作爲波紋管例如,以下詞典:Python:如何創建一個包含一些函數的字典,而不是在賦值時運行它們?
function_list = {0:power_off(), 1:image_capture(100,1000,100,100), 2:video_record(100,100), 3:image_settings(), 4:video_settings(), 5:device_settings()}
其實有大約5倍多,但我簡化了它爲這個職位... 我的問題是,我應該如何繼續,以便當我定義字典時,它不會從它的內容中運行所有的函數,而只是在我按以下方式調用它們時:function_list [current_selection],基於值current_selection參數。
我這樣做,所以我不必須有一個長期和複雜的,如果... elif的語句,如:
if current_selection == 0:
power_off()
elif current_selection == 1:
image_capture(100,1000,100,100)
elif current_selection ==2:
video_record(100,100)
... and so on ...
如果有人可以幫助我,我真的很感激它。 謝謝!
感謝提示,這應該工作。 立即測試。 – 2014-10-05 16:54:22