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 }
posted @ 2018-05-10 23:15  筱筱的春天  阅读(7027)  评论(0编辑  收藏  举报