python dict 实现 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)

你可能感兴趣的:(python dict 实现 switch 功能)