Python: 没有switch-case语句
初学Python语言,竟然很久才发现Python没有switch-case语句
官方的解释说,“用if... elif... elif... else序列很容易来实现 switch / case 语句”。而且可以使用函数字典映射和类的调度方法。
简单代码如下:
1 switch = {
2 "a":lambda x:x*2,
3 "b":lambda x:x*3,
4 "c":lambda x:x**x
5 }

浙公网安备 33010602011771号