标签:
1、python并没有提供switch语句,python可以通过字典实现switch语句的功能,实现方法分为两步
—首先,定义一个字典
—其次,调用字典的get()获取相应的表达式
通过字典调用函数
{1:case1,2:case2}.get(o)(x,y,*args,**kwargs)
2、示例
from __future__ import division def jia(x,y): return x+y def jian(x,y): return x-y def cheng(x,y): return x*y def chu(x,y): return x/y operator = {"+":jia,"-":jian,"*":cheng,"/":chu} def f(x,o,y): operator.get(o)(x,y) f(3,"/",2) #结果1.5
标签:
原文地址:http://www.cnblogs.com/sunflower627/p/4593028.html