初学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 }

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-18
  • 2022-12-23
  • 2021-08-24
  • 2021-06-12
  • 2021-05-21
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-14
  • 2022-12-23
  • 2021-11-13
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案