Python的switch实现
来源:互联网 发布:linux系统工程师培训 编辑:程序博客网 时间:2024/05/16 23:35
通常情况下,我们常用字典函数实现Python的switch语句:
#coding:utf8#add minus divide using pythonfrom __future__ import divisiondef jia(x,y): return x+ydef jian(x,y): return x-ydef cheng(x,y): return x*ydef chu(x,y): return x/yoperator={"+":jia,"-":jian,"*":cheng,"/":chu}def f(x,o,y): print(operator.get(o)(x,y))f(1,'+',3)f(1,'-',3)f(2,'*',3)f(1,'/',3)
当然,还有一种更简洁的方法:
#coding:utf8#add minus divide using pythonfrom __future__ import divisionx=1y=2operator='/'result={"+":x+y,"-":x-y,"*":x*y,"/":x/y}print(result.get(operator))
如果你理解了上面的代码,那下面代码的意思也就不难理解了:
{1:case1,2:case2}.get(x,lambda *args,**key:)()
0 0
- python switch的实现
- Python的switch实现
- python的switch实现
- python 中switch的实现
- python switch结构的实现
- Python实现类似switch的分支结构
- python switch语句的多种实现方法
- python中 switch 语句的实现
- Python中switch结构的实现
- python实现简单的switch功能
- python中实现switch
- python switch 实现
- python实现switch功能
- python 实现switch
- Python的Switch语句
- Python的switch方法
- Python中switch-case实现
- python字典实现switch功能
- [UIImage imageNamed:@""]使用误区
- C++一些注意点之new和malloc
- C语言的内存分配(参考)
- linux grep命令
- iOS内存管理
- Python的switch实现
- JSP----An internal error occurred during: "Requesting JavaScript AST from selection"
- 腾讯马拉松 威威猫系列故事——拼车记
- MySQL解决抓取文章的html标签替换及其mysql函数的用法说明
- JS XPATH示例
- linux命令
- 简单的ArrayAdapter
- 超市怎样做O2O
- [C++学习历程]Visual Studio 2010 的HelloWorld