国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看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
主站蜘蛛池模板: 国产精品午夜电影 | 欧美激情小视频 | 亚洲永久免费视频 | 精品久久国产老人久久综合 | 欧美精品国产精品 | 久久久久久夜精品精品免费 | 黄网免费看 | 亚洲美女网址 | 一区二区三区精品 | 日韩精品三区 | 国产美女精品一区二区三区 | 在线视频一区二区 | 日韩在线精品视频 | 午夜精| 欧美在线一区二区三区 | 欧美在线观看一区 | 一本一本久久a久久精品综合妖精 | 日韩欧美在线观看一区二区 | 性欧美大战久久久久久久免费观看 | 精品视频在线观看 | 欧美激情精品久久久久 | 国产精品一区二区视频 | 国产亚洲一区二区三区在线观看 | 日韩资源| 91日韩精品一区二区三区 | 精品中文字幕一区 | 一区二区三区精品 | 91精品国产一区二区 | 亚洲精品久久久一区二区三区 | 在线国产专区 | 视频一区二区三区在线观看 | 欧美日韩国产一区二区三区 | 永久免费在线 | 欧美中文字幕在线 | 91成人精品 | 精品国产不卡一区二区三区 | 国产中文字幕在线看 | 青娱乐一区 | 国产欧美一区二区精品久久 | 日韩成人在线影院 | 欧美日韩中文字幕 |