码迷,mamicode.com
首页 > 编程语言 > 详细

python基础===使用switch方法,减少使用if语句

时间:2017-08-15 22:51:22      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:get   logs   cheng   ==   语句   实现   bsp   使用   class   

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),其实就等于,jia,jian,cheng,chu 中的一种
    print(operator.get(o)(x,y))


f(4,"+",9)

#switch 实现 加减乘除的方法!

 

python基础===使用switch方法,减少使用if语句

标签:get   logs   cheng   ==   语句   实现   bsp   使用   class   

原文地址:http://www.cnblogs.com/botoo/p/7367916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!