[問題] 怎麼用 Python 寫出 switch 的功能?
大家好。
很多人都說 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
10/19 14:33, 1F
→
10/19 14:35,
8年前
, 2F
10/19 14:35, 2F
我 Python 弱手,不曉得用 class 怎麼達成 O_O"
→
10/19 14:57,
8年前
, 3F
10/19 14:57, 3F
是啊,不過有時候可以反過來利用這特性,不是嗎?
→
10/19 15:09,
8年前
, 4F
10/19 15:09, 4F
沒有唷
※ 編輯: henry8168 (60.248.17.229), 10/19/2017 15:14:16
推
10/19 17:28,
8年前
, 5F
10/19 17:28, 5F
→
10/19 18:09,
8年前
, 6F
10/19 18:09, 6F
→
10/19 18:09,
8年前
, 7F
10/19 18:09, 7F
→
10/19 18:16,
8年前
, 8F
10/19 18:16, 8F
→
10/19 18:38,
8年前
, 9F
10/19 18:38, 9F
→
10/19 18:41,
8年前
, 10F
10/19 18:41, 10F
推
10/19 19:49,
8年前
, 11F
10/19 19:49, 11F
→
10/19 20:30,
8年前
, 12F
10/19 20:30, 12F
→
10/19 20:32,
8年前
, 13F
10/19 20:32, 13F
→
10/19 20:32,
8年前
, 14F
10/19 20:32, 14F
→
10/19 20:55,
8年前
, 15F
10/19 20:55, 15F
→
10/19 20:55,
8年前
, 16F
10/19 20:55, 16F
→
10/19 20:55,
8年前
, 17F
10/19 20:55, 17F
→
10/19 20:55,
8年前
, 18F
10/19 20:55, 18F
→
10/19 21:06,
8年前
, 19F
10/19 21:06, 19F
→
10/19 21:06,
8年前
, 20F
10/19 21:06, 20F
→
10/19 23:11,
8年前
, 21F
10/19 23:11, 21F
推
10/20 00:06,
8年前
, 22F
10/20 00:06, 22F
→
10/20 00:07,
8年前
, 23F
10/20 00:07, 23F
→
10/20 00:08,
8年前
, 24F
10/20 00:08, 24F
→
10/20 00:08,
8年前
, 25F
10/20 00:08, 25F
→
10/20 00:34,
8年前
, 26F
10/20 00:34, 26F
推
10/20 05:16,
8年前
, 27F
10/20 05:16, 27F
→
10/20 05:16,
8年前
, 28F
10/20 05:16, 28F
→
10/20 05:16,
8年前
, 29F
10/20 05:16, 29F
→
10/20 09:43,
8年前
, 30F
10/20 09:43, 30F
→
10/20 09:44,
8年前
, 31F
10/20 09:44, 31F
→
10/20 09:44,
8年前
, 32F
10/20 09:44, 32F
→
10/20 12:23,
8年前
, 33F
10/20 12:23, 33F
→
10/20 12:23,
8年前
, 34F
10/20 12:23, 34F
推
10/20 13:24,
8年前
, 35F
10/20 13:24, 35F
→
10/20 13:25,
8年前
, 36F
10/20 13:25, 36F
→
10/20 13:25,
8年前
, 37F
10/20 13:25, 37F
→
10/20 14:29,
8年前
, 38F
10/20 14:29, 38F
推
10/20 15:46,
8年前
, 39F
10/20 15:46, 39F
有 break 的 switch 才像一連串的 if else 吧
※ 編輯: henry8168 (219.70.252.12), 10/24/2017 22:35:31
討論串 (同標題文章)
以下文章回應了本文 (最舊先):
完整討論串 (本文為第 1 之 11 篇):