看板 [ Python ]
討論串[問題] 怎麼用 Python 寫出 switch 的功能?
共 11 篇文章

推噓0(0推 0噓 0→)留言0則,0人參與, 最新作者mikapauli (桜花)時間8年前 (2017/10/19 20:18), 編輯資訊
0
0
0
內容預覽:
使用dict的方法:. def f0():. .... def f1():. .... def f2():. .... .... switch = {'c0': 0, 'c1': 1, 'c2': 2, ...}. process = (f0, f1, f2, ...). .... [f() for
(還有78個字)

推噓2(2推 0噓 1→)留言3則,0人參與, 8年前最新作者rexyeah (ccccccc)時間8年前 (2017/10/19 17:19), 編輯資訊
0
0
0
內容預覽:
不知道這樣有符合嗎?. # predefined init process. def init1():. print 'init1'. def init2():. print 'init2'. def init3():. print 'init3'. # mimic the switch fallt
(還有120個字)

推噓1(1推 0噓 4→)留言5則,0人參與, 8年前最新作者henry8168 (番薯猴)時間8年前 (2017/10/19 16:26), 8年前編輯資訊
0
0
1
內容預覽:
成功了!參考 kenduest 大的回文,. 我嘗試改用 list 的結構去寫,變成如下:. https://gist.github.com/henry8168/9192ee32e7859598d02f655895e45df3. 可以嘗試將不同 init_process 裡面的 return 改成負
(還有327個字)

推噓0(0推 0噓 2→)留言2則,0人參與, 8年前最新作者lizkarina (lizka)時間8年前 (2017/10/19 15:55), 8年前編輯資訊
0
0
0
內容預覽:
修改後,有保證順序。. from collections import OrderedDict. entry = OrderedDict(). entry["init_process3"] = release_process2. entry["init_process2"] = release_pr
(還有862個字)

推噓0(0推 0噓 2→)留言2則,0人參與, 8年前最新作者kenduest (小州)時間8年前 (2017/10/19 14:44), 8年前編輯資訊
0
0
0
內容預覽:
類似這個方式嗎?. entry = { "init_process3": release_process2,. "init_process2": release_process1,. "init_process2": initial_fail,. }. handler = entry.get(err
(還有489個字)