python & dict & switch

时间:2023-01-06 14:45:01

python & dict & switch

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

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 30 22:12:43 2019 @author: xgqfrms-mbp
""" #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