Python中Swithch Case语法实现

其他语言中,switch语句大概是这样的

  
  
  1. switch (var)
  2. {
  3.     case value1: do_some_stuff1();
  4.     case value2: do_some_stuff2();
  5.     ...
  6.     case valueN: do_some_stuffN();
  7.     default: do_default_stuff();
  8. }
而python本身没有switch语句,解决方法有以下3种: A.使用dictionary values = { value1: do_some_stuff1, value2: do_some_stuff2, ... valueN: do_some_stuffN, } values.get(var, do_default_stuff)() B.使用lambda result = { 'a': lambda x: x * 5, 'b': lambda x: x + 7, 'c': lambda x: x - 2 }[value](x) C.Brian Beck提供了一个类 switch 来实现其他语言中switch的功能
  1. # This class provides the functionality we want. You only need to look at
  2. # this if you want to know how this works. It only needs to be defined
  3. # once, no need to muck around with its internals.
  4. class switch(object):
  5.     def __init__(self, value):
  6.         self.value = value
  7.         self.fall = False
  8.     def __iter__(self):
  9.         """Return the match method once, then stop"""
  10.         yield self.match
  11.         raise StopIteration
  12.     def match(self, *args):
  13.         """Indicate whether or not to enter a case suite"""
  14.         if self.fall or not args:
  15.             return True
  16.         elif self.value in args: # changed for v1.5, see below
  17.             self.fall = True
  18.             return True
  19.         else:
  20.             return False
  21. # The following example is pretty much the exact use-case of a dictionary,
  22. # but is included for its simplicity. Note that you can include statements
  23. # in each suite.
  24. v = 'ten'
  25. for case in switch(v):
  26.     if case('one'):
  27.         print 1
  28.         break
  29.     if case('two'):
  30.         print 2
  31.         break
  32.     if case('ten'):
  33.         print 10
  34.         break
  35.     if case('eleven'):
  36.         print 11
  37.         break
  38.     if case(): # default, could also just omit condition or 'if True'
  39.         print "something else!"
  40.         # No need to break here, it'll stop anyway

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

[python]  view plain copy
  1. #coding: utf-8  
  2. from __future__ import division  
  3.   
  4. def jia(x,y):  
  5.     print x+y  
  6.   
  7. def jian(x,y):  
  8.     print x-y  
  9.   
  10. def cheng(x,y):  
  11.     print x*y  
  12.   
  13. def chu(x,y):  
  14.     print x/y  
  15.   
  16. operator = {'+':jia,'-':jian,'*':cheng,'/':chu}  
  17.   
  18. def f(x,o,y):  
  19.     operator.get(o)(x,y)  
  20.   
  21. f(3,'+',2)  

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值