Re: [問題] 怎麼用 Python 寫出 switch 的功能?

作者: kenduest (小州)   2017-10-19 14:44:47
※ 引述《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
作者: djshen (djshen)   2017-10-19 15:06:00
他需要init_process3發生時也要跑2和1
作者: MasterChang (我愛ASM)   2017-10-20 16:11:00
大大很久沒出現囉!!

Links booklink

Contact Us: admin [ a t ] ucptt.com