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

看板Python作者 (番薯猴)時間8年前 (2017/10/19 14:28), 8年前編輯推噓7(7032)
留言39則, 9人參與, 8年前最新討論串1/11 (看更多)
大家好。 很多人都說 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://www.ptt.cc/bbs/Python/M.1508394515.A.5E8.html ※ 編輯: henry8168 (60.248.17.229), 10/19/2017 14:30:41

10/19 14:33, 8年前 , 1F
我覺得運用Switch-Case不break的特性 並不是一個好的寫法
10/19 14:33, 1F

10/19 14:35, 8年前 , 2F
閱讀的直覺性不夠. 但到了Python要寫得漂亮要包class吧
10/19 14:35, 2F
我 Python 弱手,不曉得用 class 怎麼達成 O_O"

10/19 14:57, 8年前 , 3F
swich-case fallthrough 是很多 bug 的根源欸...
10/19 14:57, 3F
是啊,不過有時候可以反過來利用這特性,不是嗎?

10/19 15:09, 8年前 , 4F
case之間有dependency嗎?
10/19 15:09, 4F
沒有唷 ※ 編輯: henry8168 (60.248.17.229), 10/19/2017 15:14:16

10/19 17:28, 8年前 , 5F
看起來你需要的是with statement
10/19 17:28, 5F

10/19 18:09, 8年前 , 6F
覺得樓上正解,你需要的應該是 with 沒錯。每個語言特性
10/19 18:09, 6F

10/19 18:09, 8年前 , 7F
不同,照般 C 的作法不見得比較好。
10/19 18:09, 7F

10/19 18:16, 8年前 , 8F
如果是用with,大概該怎麼做?
10/19 18:16, 8F

10/19 18:38, 8年前 , 9F
大概像這樣 https://git.io/vd7iE
10/19 18:38, 9F

10/19 18:41, 8年前 , 10F
謝謝。
10/19 18:41, 10F

10/19 19:49, 8年前 , 11F
我大概會寫成這樣 https://ideone.com/0WShEj
10/19 19:49, 11F

10/19 20:30, 8年前 , 12F
@stucode 原PO沒有error的時候return 0 所以不能用finally
10/19 20:30, 12F

10/19 20:32, 8年前 , 13F
另外問一下 init_phase裡面會有多個地方都goto fail_p1?
10/19 20:32, 13F

10/19 20:32, 8年前 , 14F
還有release重複執行會不會有問題
10/19 20:32, 14F

10/19 20:55, 8年前 , 15F
@djshen 不太懂 return 0 跟 finally 之間的關係@@。
10/19 20:55, 15F

10/19 20:55, 8年前 , 16F
原 PO 之所以 return 0 應該是因為走 C 傳統用 return
10/19 20:55, 16F

10/19 20:55, 8年前 , 17F
value 判斷錯誤的處理架構,我這邊則是用 exception 做
10/19 20:55, 17F

10/19 20:55, 8年前 , 18F
錯誤處理控制,非要 return 0 不可的話也可以做些修改。
10/19 20:55, 18F

10/19 21:06, 8年前 , 19F
啊,好像懂了。你是指在 init_phase 成功的話,之後就不
10/19 21:06, 19F

10/19 21:06, 8年前 , 20F
會再 call release 的意思嗎?
10/19 21:06, 20F

10/19 23:11, 8年前 , 21F
對 沒有表達得很好
10/19 23:11, 21F

10/20 00:06, 8年前 , 22F
正常來說,return 0出去外面還是要有人call release()
10/20 00:06, 22F

10/20 00:07, 8年前 , 23F
python style就會變成stucode寫的main()那樣
10/20 00:07, 23F

10/20 00:08, 8年前 , 24F
外面用完回到context manager做release
10/20 00:08, 24F

10/20 00:08, 8年前 , 25F
所以這樣就ok了
10/20 00:08, 25F

10/20 00:34, 8年前 , 26F
這樣說也是有道理
10/20 00:34, 26F

10/20 05:16, 8年前 , 27F
同 AstralBrain 大看法。如果真的有出 init 就不 release
10/20 05:16, 27F

10/20 05:16, 8年前 , 28F
的需求的話,加個 flag 變數或是自訂 exception 類別也可
10/20 05:16, 28F

10/20 05:16, 8年前 , 29F
以達成。
10/20 05:16, 29F

10/20 09:43, 8年前 , 30F
翻了一下文件發現contextlib.ExitStack跟我的想法很接近
10/20 09:43, 30F

10/20 09:44, 8年前 , 31F
把callback存到stack, exit的時候執行
10/20 09:44, 31F

10/20 09:44, 8年前 , 32F
不喜歡寫太多nested with
10/20 09:44, 32F

10/20 12:23, 8年前 , 33F
疊太多 with 的確是個問題。想了一下,原 PO 的 case 也
10/20 12:23, 33F

10/20 12:23, 8年前 , 34F
許真的比較適合用 ExitStack。 https://git.io/vdd2Z
10/20 12:23, 34F

10/20 13:24, 8年前 , 35F
with 也可以不用那麼多層啊
10/20 13:24, 35F

10/20 13:25, 8年前 , 36F
with init1(), init2(), init3(): do_something()
10/20 13:25, 36F

10/20 13:25, 8年前 , 37F
當然如果每個init中間都還要做點其他事就沒辦法了
10/20 13:25, 37F

10/20 14:29, 8年前 , 38F
我差點以為是網路的switch
10/20 14:29, 38F

10/20 15:46, 8年前 , 39F
請問一下不break的switch跟用一連串if有什麼不同?
10/20 15:46, 39F
有 break 的 switch 才像一連串的 if else 吧 ※ 編輯: henry8168 (219.70.252.12), 10/24/2017 22:35:31
文章代碼(AID): #1Pw4OJNe (Python)
討論串 (同標題文章)
以下文章回應了本文 (最舊先):
完整討論串 (本文為第 1 之 11 篇):
文章代碼(AID): #1Pw4OJNe (Python)