作者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://webptt.com/cn.aspx?n=bbs/Python/M.1508395491.A.521.html
1F:→ 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
2F:→ MasterChang: 大大很久没出现罗!! 10/20 16:11