python实现类似switch功能

下面是python实现了类似其他语言switch功能代码

#!/usr/bin/python
#coding:utf-8

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):
    print operator.get(o)(x,y)

f(3,"+",2)

你可能感兴趣的:(python)