国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - python中Switch/Case實現的示例代碼

python中Switch/Case實現的示例代碼

2020-12-16 00:59gerrydeng Python

本篇文章主要介紹了python中Switch/Case實現的示例代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

學習Python過程中,發現沒有switch-case,過去寫C習慣用Switch/Case語句,官方文檔說通過if-elif實現。所以不妨自己來實現Switch/Case功能。

使用if…elif…elif…else 實現switch/case

可以使用if…elif…elif..else序列來代替switch/case語句,這是大家最容易想到的辦法。但是隨著分支的增多和修改的頻繁,這種代替方式并不很好調試和維護。

方法一

通過字典實現

?
1
2
3
4
5
6
def foo(var):
  return {
      'a': 1
      'b': 2,
      'c': 3,
  }.get(var,'error'#'error'為默認返回值,可自設置

方法二

通過匿名函數實現

?
1
2
3
4
5
6
def foo(var,x):
  return {
      'a': lambda x: x+1,
      'b': lambda x: x+2,
      'c': lambda x: x+3,
  }[var](x)

方法三

通過定義類實現

參考Brian Beck通過類來實現Swich-case

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# This class provides the functionality we want. You only need to look at
# this if you want to know how this works. It only needs to be defined
# once, no need to muck around with its internals.
class switch(object):
  def __init__(self, value):
    self.value = value
    self.fall = False
 
  def __iter__(self):
    """Return the match method once, then stop"""
    yield self.match
    raise StopIteration
 
  def match(self, *args):
    """Indicate whether or not to enter a case suite"""
    if self.fall or not args:
      return True
    elif self.value in args: # changed for v1.5, see below
      self.fall = True
      return True
    else:
      return False
 
 
# The following example is pretty much the exact use-case of a dictionary,
# but is included for its simplicity. Note that you can include statements
# in each suite.
v = 'ten'
for case in switch(v):
  if case('one'):
    print 1
    break
  if case('two'):
    print 2
    break
  if case('ten'):
    print 10
    break
  if case('eleven'):
    print 11
    break
  if case(): # default, could also just omit condition or 'if True'
    print "something else!"
    # No need to break here, it'll stop anyway
 
# break is used here to look as much like the real thing as possible, but
# elif is generally just as good and more concise.
 
# Empty suites are considered syntax errors, so intentional fall-throughs
# should contain 'pass'
c = 'z'
for case in switch(c):
  if case('a'): pass # only necessary if the rest of the suite is empty
  if case('b'): pass
  # ...
  if case('y'): pass
  if case('z'):
    print "c is lowercase!"
    break
  if case('A'): pass
  # ...
  if case('Z'):
    print "c is uppercase!"
    break
  if case(): # default
    print "I dunno what c was!"
 
# As suggested by Pierre Quentel, you can even expand upon the
# functionality of the classic 'case' statement by matching multiple
# cases in a single shot. This greatly benefits operations such as the
# uppercase/lowercase example above:
import string
c = 'A'
for case in switch(c):
  if case(*string.lowercase): # note the * for unpacking as arguments
    print "c is lowercase!"
    break
  if case(*string.uppercase):
    print "c is uppercase!"
    break
  if case('!', '?', '.'): # normal argument passing style also applies
    print "c is a sentence terminator!"
    break
  if case(): # default
    print "I dunno what c was!"
 
# Since Pierre's suggestion is backward-compatible with the original recipe,
# I have made the necessary modification to allow for the above usage.

查看Python官方:PEP 3103-A Switch/Case Statement

發現其實實現Switch Case需要被判斷的變量是可哈希的和可比較的,這與Python倡導的靈活性有沖突。在實現上,優化不好做,可能到最后最差的情況匯編出來跟If Else組是一樣的。所以Python沒有支持。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.cnblogs.com/gerrydeng/p/7191927.html

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 91综合网| 一区二区av | 日韩av专区 | 欧美激情一区二区三级高清视频 | 国产高清精品在线 | 国产精品久久久久久久久图文区 | 国产精品一区久久久 | 久草在线 | 婷婷色视频 | 日韩在线小视频 | 伊人久久综合精品一区二区三区 | 国产精品福利电影网 | 久久中文精品 | 国产午夜精品福利 | 亚洲国产成人精品女 | 国产美女一区二区三区 | 国产精品五区 | 国产精品成人国产乱一区 | 日韩美女av在线 | www.国产一区 | 欧美国产在线观看 | 国产女爽爽视频精品免费 | 久久er99热精品一区二区 | 日韩成人免费中文字幕 | 精品欧美一区二区三区久久久 | 丁香伊人| 欧美精品1区2区 | 欧美视频一区二区 | 国产欧美久久久久久 | 亚洲精彩视频 | 最近免费中文字幕大全免费版视频 | 亚洲成人第一网站 | 日本精品一区二 | 自拍偷拍第一页 | 日韩精品在线视频 | 91在线综合 | 久久精品国产亚洲一区二区三区 | 亚洲一区二区三区四区的 | 五月天婷婷综合 | 国产精品永久免费视频 | 日韩看片 |