摘要:
python中没有switch语句,但是可以灵活实现。下面就是一种实现:def f(x): def case1(): pass def case2(): pass {1: case1, 2: case2}[x]()但是更安全的写法是:def f(x): def case1(): pass def case2(): pass d = {1: case1, 2: case2} deffunc = lambda *args, **kw: None func = d.get(x, deffunc) ... 阅读全文