阅读180 返回首页    go 阿里云 go 技术社区[云栖]


python中switch语句用法

python中是没用switch语句的,这应该是体现python大道至简的思想,python中一般多用字典来代替switch来实现。

#coding: utf-8
from __future__ import division

def jia(x,y):
    print x+y

def jian(x,y):
    print x-y

def cheng(x,y):
    print x*y

def chu(x,y):
    print x/y

operator = {'+':jia,'-':jian,'*':cheng,'/':chu}

def f(x,o,y):
    operator.get(o)(x,y)

f(3,'+',2)

上面的代码就用字典实现了选择功能,要是在C++中实现上述功能,是用switch实现的,但是用python的字典实现起来更简单

最后更新:2017-04-03 16:48:56

  上一篇:go Linux下安装jdk报Permission denied以及chmod详解
  下一篇:go python网络爬虫抓取图片