作者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/m.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