看板
[ Python ]
討論串[問題] 怎麼用 Python 寫出 switch 的功能?
共 11 篇文章
內容預覽:
使用dict的方法:. def f0():. .... def f1():. .... def f2():. .... .... switch = {'c0': 0, 'c1': 1, 'c2': 2, ...}. process = (f0, f1, f2, ...). .... [f() for
(還有78個字)
內容預覽:
不知道這樣有符合嗎?. # predefined init process. def init1():. print 'init1'. def init2():. print 'init2'. def init3():. print 'init3'. # mimic the switch fallt
(還有120個字)
內容預覽:
成功了!參考 kenduest 大的回文,. 我嘗試改用 list 的結構去寫,變成如下:. https://gist.github.com/henry8168/9192ee32e7859598d02f655895e45df3. 可以嘗試將不同 init_process 裡面的 return 改成負
(還有327個字)
內容預覽:
修改後,有保證順序。. from collections import OrderedDict. entry = OrderedDict(). entry["init_process3"] = release_process2. entry["init_process2"] = release_pr
(還有862個字)
內容預覽:
類似這個方式嗎?. entry = { "init_process3": release_process2,. "init_process2": release_process1,. "init_process2": initial_fail,. }. handler = entry.get(err
(還有489個字)