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