作者kenduest (小州)
看板Python
標題Re: [問題] 怎麼用 Python 寫出 switch 的功能?
時間Thu Oct 19 14:44:47 2017
※ 引述《henry8168 (番薯猴)》之銘言:
: switch(errfunc){
: case "init_process3":
: release_process2();
: case "init_process2":
: release_process1();
: case "init_process1":
: printf("%s: initial failed.\n",errfunc);
: }
: return -1;
: }
: 抱歉,在 Python 板打這麼多 C 語言 @@"
: 不過我想表達的就如同上述,請問 Python 該怎麼做到類似的功能呢?
: 我正在改一位同仁的 Python,想運用類似 switch 的特點完成。
: 查到很多人都說可以用 dict,卻還是一頭霧水。
: 謝謝。
類似這個方式嗎?
entry = { "init_process3": release_process2,
"init_process2": release_process1,
"init_process2": initial_fail,
}
handler = entry.get(errfunc)
if handler:
handler()
return -1
--
如果真的愛一個作業系統 怎能不害怕不再能使用它 而我們必須接受現實
於是長大了 寂寞就是沒有了機會再用它 比沒有電腦還要寂寞
而那些點亮我們生命的作業系統 就彷彿電影中發生在暹邏的愛情故事
即使短暫使用也會刻骨銘心
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.117.155.17
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1508395491.A.521.html
→ djshen: 他需要init_process3發生時也要跑2和1 10/19 15:06
那沒有好方式,自己包裝一下比較快... 爛方式...
entry = { "init_process3": [release_process2,release_process1],
"init_process2": [release_process1],
"init_process2": [initial_fail],
}
handler = entry.get(errfunc)
if handler:
for item in handler:
item()
else:
return -1
※ 編輯: kenduest (122.117.155.17), 10/19/2017 15:44:05
→ MasterChang: 大大很久沒出現囉!! 10/20 16:11