作者henry8168 (番薯猴)
看板Python
标题[问题] 怎麽用 Python 写出 switch 的功能?
时间Thu Oct 19 14:28:31 2017
大家好。
很多人都说 if else 已经很够用了,不需要 switch,
但 C 语言的 switch 有一个特点就是,如果每个 case 不加上 break,
就能够继续执行下方其他 case 的行为。
尤其写 kernel module 的人,
应该常常会需要在 initial 阶段控管初始化失败时的状况。
打个比方,很多开发者都会很喜欢用 goto。
int init_phase(){
char* errfunc = "functionNameHere";
if(init_process1() < 0){
errfunc = "init_process1";
goto fail_p1;
}
if(init_process2() < 0){
errfunc = "init_process2";
goto fail_p2;
}
if(init_process3() < 0){
errfunc = "init_process3";
goto fail_p3;
}
return 0;
init_process3:
release_process2();
init_process2:
release_process1();
init_process1:
printf("%s: initial failed.\n",errfunc);
return -1;
}
但不爱用 goto 的我就会改成以下:
int init_phase(){
char* errfunc = "functionNameHere";
int errOccur = 0;
if(!errOccur){
if(init_process1() < 0){
errfunc = "init_process1";
errOccur = 1;
}
}
if(!errOccur){
if(init_process2() < 0){
errfunc = "init_process2";
errOccur = 1;
}
}
if(!errOccur){
if(init_process3() < 0){
errfunc = "init_process3";
errOccur = 1;
}
}
if(!errOccur)
return 0;
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,却还是一头雾水。
谢谢。
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 60.248.17.229
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1508394515.A.5E8.html
※ 编辑: henry8168 (60.248.17.229), 10/19/2017 14:30:41
1F:推 Yshuan: 我觉得运用Switch-Case不break的特性 并不是一个好的写法 10/19 14:33
2F:→ Yshuan: 阅读的直觉性不够. 但到了Python要写得漂亮要包class吧 10/19 14:35
我 Python 弱手,不晓得用 class 怎麽达成 O_O"
3F:→ uranusjr: swich-case fallthrough 是很多 bug 的根源欸... 10/19 14:57
是啊,不过有时候可以反过来利用这特性,不是吗?
4F:→ djshen: case之间有dependency吗? 10/19 15:09
没有唷
※ 编辑: henry8168 (60.248.17.229), 10/19/2017 15:14:16
5F:推 AstralBrain: 看起来你需要的是with statement 10/19 17:28
6F:→ stucode: 觉得楼上正解,你需要的应该是 with 没错。每个语言特性 10/19 18:09
7F:→ stucode: 不同,照般 C 的作法不见得比较好。 10/19 18:09
8F:→ lizkarina: 如果是用with,大概该怎麽做? 10/19 18:16
10F:→ lizkarina: 谢谢。 10/19 18:41
12F:→ djshen: @stucode 原PO没有error的时候return 0 所以不能用finally 10/19 20:30
13F:→ djshen: 另外问一下 init_phase里面会有多个地方都goto fail_p1? 10/19 20:32
14F:→ djshen: 还有release重复执行会不会有问题 10/19 20:32
15F:→ stucode: @djshen 不太懂 return 0 跟 finally 之间的关系@@。 10/19 20:55
16F:→ stucode: 原 PO 之所以 return 0 应该是因为走 C 传统用 return 10/19 20:55
17F:→ stucode: value 判断错误的处理架构,我这边则是用 exception 做 10/19 20:55
18F:→ stucode: 错误处理控制,非要 return 0 不可的话也可以做些修改。 10/19 20:55
19F:→ stucode: 啊,好像懂了。你是指在 init_phase 成功的话,之後就不 10/19 21:06
20F:→ stucode: 会再 call release 的意思吗? 10/19 21:06
21F:→ djshen: 对 没有表达得很好 10/19 23:11
22F:推 AstralBrain: 正常来说,return 0出去外面还是要有人call release() 10/20 00:06
23F:→ AstralBrain: python style就会变成stucode写的main()那样 10/20 00:07
24F:→ AstralBrain: 外面用完回到context manager做release 10/20 00:08
25F:→ AstralBrain: 所以这样就ok了 10/20 00:08
26F:→ djshen: 这样说也是有道理 10/20 00:34
27F:推 stucode: 同 AstralBrain 大看法。如果真的有出 init 就不 release 10/20 05:16
28F:→ stucode: 的需求的话,加个 flag 变数或是自订 exception 类别也可 10/20 05:16
29F:→ stucode: 以达成。 10/20 05:16
30F:→ djshen: 翻了一下文件发现contextlib.ExitStack跟我的想法很接近 10/20 09:43
31F:→ djshen: 把callback存到stack, exit的时候执行 10/20 09:44
32F:→ djshen: 不喜欢写太多nested with 10/20 09:44
33F:→ stucode: 叠太多 with 的确是个问题。想了一下,原 PO 的 case 也 10/20 12:23
35F:推 AstralBrain: with 也可以不用那麽多层啊 10/20 13:24
36F:→ AstralBrain: with init1(), init2(), init3(): do_something() 10/20 13:25
37F:→ AstralBrain: 当然如果每个init中间都还要做点其他事就没办法了 10/20 13:25
38F:→ ym7834: 我差点以为是网路的switch 10/20 14:29
39F:推 kita: 请问一下不break的switch跟用一连串if有什麽不同? 10/20 15:46
有 break 的 switch 才像一连串的 if else 吧
※ 编辑: henry8168 (219.70.252.12), 10/24/2017 22:35:31