我想用Python编写一个函数,该函数根据输入索引的值返回不同的固定值。
在其他语言中,我将使用switchor case语句,但Python似乎没有switch语句。在这种情况下,推荐的Python解决方案是什么?
switch实际上,它比根据输入索引的值返回不同的固定值的东西更“通用”。它允许执行不同的代码段。实际上,它甚至不需要返回任何值。我不知道这里的某些答案是对通用switch语句的良好替代,还是仅对于无法执行通用代码的返回值的情况。
                我想用Python编写一个函数,该函数根据输入索引的值返回不同的固定值。
在其他语言中,我将使用switchor case语句,但Python似乎没有switch语句。在这种情况下,推荐的Python解决方案是什么?
switch实际上,它比根据输入索引的值返回不同的固定值的东西更“通用”。它允许执行不同的代码段。实际上,它甚至不需要返回任何值。我不知道这里的某些答案是对通用switch语句的良好替代,还是仅对于无法执行通用代码的返回值的情况。
                Answers:
您可以使用字典:
def f(x):
    return {
        'a': 1,
        'b': 2,
    }[x]get方法可能比使用collections.defaultdict在这种情况下。
                    }.get(x, default)如果应该使用默认值,请执行此操作。(注意:这比将默认值保留为switch语句时要好得多!)
                    如果您想要默认值,可以使用字典 get(key[, default])方法:
def f(x):
    return {
        'a': 1,
        'b': 2
    }.get(x, 9)    # 9 is default if x not found.get()(如当前的最高答案)将需要在发送之前急切地评估所有可能性,因此,不仅效率非常低而且不仅没有副作用,而且还没有副作用。这个答案绕过了这个问题,但更为冗长。我只会使用if / elif / else,甚至那些只要写'case'就需要很长时间。
                    [value],该字典将仅返回3个函数之一(假设value是3个键之一)。该函数尚未被调用。然后(x)使用x作为参数调用刚返回的函数(结果转到result)。其他2个函数将不会被调用。
                    除了字典方法(我真的很喜欢,BTW),你也可以使用if- elif- else获取switch/ case/ default功能:
if x == 'a':
    # Do the thing
elif x == 'b':
    # Do the other thing
if x in 'bc':
    # Fall-through by not using elif, but now the default case includes case 'a'!
elif x in 'xyz':
    # Do yet another thing
else:
    # Do the default当然,这与switch / case并不相同-您不能像放弃该break语句那样容易地遇到故障,但是您可以进行更复杂的测试。它的格式比一系列nested更好if,即使从功能上来说,它也更接近。
get方式,但是标准方式更具可读性。
                    x = the.other.thing之前。通常,您会有一个if,多个elif和一个else,因为这更容易理解。
                    if/elif/else?
                    x in 'bc',请记住"" in "bc"是True。
                    我最喜欢的用于switch / case的Python配方是:
choices = {'a': 1, 'b': 2}
result = choices.get(key, 'default')简单的场景简单明了。
比较11行以上的C代码:
// C Language version of a simple 'switch/case'.
switch( key ) 
{
    case 'a' :
        result = 1;
        break;
    case 'b' :
        result = 2;
        break;
    default :
        result = -1;
}您甚至可以使用元组分配多个变量:
choices = {'a': (1, 2, 3), 'b': (4, 5, 6)}
(result1, result2, result3) = choices.get(key, ('default1', 'default2', 'default3'))default = -1; result = choices.get(key, default)。
                    result=key=='a'?1:key==b?2:-1
                    result = 1 if key == 'a' else (2 if key == 'b' else 'default')。但是那只班轮可读吗?
                    class switch(object):
    value = None
    def __new__(class_, value):
        class_.value = value
        return True
def case(*args):
    return any((arg == switch.value for arg in args))用法:
while switch(n):
    if case(0):
        print "You typed zero."
        break
    if case(1, 4, 9):
        print "n is a perfect square."
        break
    if case(2):
        print "n is an even number."
    if case(2, 3, 5, 7):
        print "n is a prime number."
        break
    if case(6, 8):
        print "n is an even number."
        break
    print "Only single-digit numbers are allowed."
    break测试:
n = 2
#Result:
#n is an even number.
#n is a prime number.
n = 11
#Result:
#Only single-digit numbers are allowed.if语句不是更有效吗?
                    case(2)块调用了另一个使用switch()的函数,则在执行case(2, 3, 5, 7)etc等操作查找下一个情况时,它将使用另一个函数设置的开关值,而不是当前switch语句设置的开关值。
                    我最喜欢的是一个非常好的食谱。您会非常喜欢它。这是我所见过的最接近实际开关案例的陈述,尤其是在功能方面。
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!"for case in switch()用with switch() as case,更有意义,因为它需要s到只运行一次。
                    with是不允许的break,因此将删除掉选项。
                    if c in set(range(0,9)): print "digit" elif c in set(map(chr, range(ord('a'), ord('z')))): print "lowercase"吗?
                    class Switch:
    def __init__(self, value):
        self.value = value
    def __enter__(self):
        return self
    def __exit__(self, type, value, traceback):
        return False # Allows a traceback to occur
    def __call__(self, *values):
        return self.value in values
from datetime import datetime
with Switch(datetime.today().weekday()) as case:
    if case(0):
        # Basic usage of switch
        print("I hate mondays so much.")
        # Note there is no break needed here
    elif case(1,2):
        # This switch also supports multiple conditions (in one line)
        print("When is the weekend going to be here?")
    elif case(3,4):
        print("The weekend is near.")
    else:
        # Default would occur here
        print("Let's go have fun!") # Didn't use case for example purposesvalue在Switch类中添加(公共)属性,以便您可以case.value在语句中引用。
                    我从Twisted Python代码中学到了一种模式。
class SMTP:
    def lookupMethod(self, command):
        return getattr(self, 'do_' + command.upper(), None)
    def do_HELO(self, rest):
        return 'Howdy ' + rest
    def do_QUIT(self, rest):
        return 'Bye'
SMTP().lookupMethod('HELO')('foo.bar.com') # => 'Howdy foo.bar.com'
SMTP().lookupMethod('QUIT')('') # => 'Bye'您可以在需要分发令牌并执行扩展代码的任何时间使用它。在状态机中,您将拥有state_方法,并在上调度self.state。通过从基类继承并定义您自己的do_方法,可以完全扩展此开关。通常,您甚至do_在基类中甚至都没有方法。
编辑:到底使用了什么
如果是SMTP,您将从HELO网上收到。相关代码(来自twisted/mail/smtp.py,针对我们的情况进行了修改)如下所示
class SMTP:
    # ...
    def do_UNKNOWN(self, rest):
        raise NotImplementedError, 'received unknown command'
    def state_COMMAND(self, line):
        line = line.strip()
        parts = line.split(None, 1)
        if parts:
            method = self.lookupMethod(parts[0]) or self.do_UNKNOWN
            if len(parts) == 2:
                return method(parts[1])
            else:
                return method('')
        else:
            raise SyntaxError, 'bad syntax'
SMTP().state_COMMAND('   HELO   foo.bar.com  ') # => Howdy foo.bar.com您会收到'  HELO   foo.bar.com '(或者您可能会收到'QUIT'或'RCPT TO: foo')。这被标记为partsas ['HELO', 'foo.bar.com']。实际的方法查找名称取自parts[0]。
(原始方法也称为state_COMMAND,因为它使用相同的模式来实现状态机,即getattr(self, 'state_' + self.mode))
假设您不想只返回一个值,而是想使用更改对象上某些内容的方法。使用此处说明的方法将是:
result = {
  'a': obj.increment(x),
  'b': obj.decrement(x)
}.get(value, obj.default(x))这里发生的是python评估字典中的所有方法。因此,即使您的值为'a',该对象也会被递增, x减少x。
解:
func, args = {
  'a' : (obj.increment, (x,)),
  'b' : (obj.decrement, (x,)),
}.get(value, (obj.default, (x,)))
result = func(*args)因此,您将获得一个包含函数及其参数的列表。这样,仅返回函数指针和参数列表,而不对其求值。然后,“结果”评估返回的函数调用。
我只想在这里放两分钱。Python中没有case / switch语句的原因是因为Python遵循“只有一种正确的方法来做某事”的原则。因此,显然您可以想出各种重新创建开关/案例功能的方法,但是实现此功能的Python方法是if / elif构造。即
if something:
    return "first thing"
elif somethingelse:
    return "second thing"
elif yetanotherthing:
    return "third thing"
else:
    return "default thing"我只是觉得PEP 8应该在这里点头。Python的美丽之处之一就是它的简洁和优雅。这很大程度上源于PEP 8中规定的原则,包括“做某事的唯一正确方法”
扩展“ dict as switch”的想法。如果要为开关使用默认值:
def f(x):
    try:
        return {
            'a': 1,
            'b': 2,
        }[x]
    except KeyError:
        return 'default''default'规则(从此dict中得到一些东西)的例外(回退)。按照设计,Python程序一经使用就会使用异常。话虽这么说,使用get可能会使代码变得更好。
                    如果您有一个复杂的case块,可以考虑使用函数字典查找表...
如果您尚未执行此操作,那么最好进入调试器并准确查看字典如何查找每个函数。
注意:千万不能使用“()”的情况下/字典查找内部或它会调用每个函数被创建字典/ case块。请记住这一点,因为您只想使用哈希样式查找一次调用每个函数。
def first_case():
    print "first"
def second_case():
    print "second"
def third_case():
    print "third"
mycase = {
'first': first_case, #do not use ()
'second': second_case, #do not use ()
'third': third_case #do not use ()
}
myfunc = mycase['first']
myfunc()如果要搜索额外的语句(例如“ switch”),则构建了扩展Python的python模块。叫做ESPY称为“ Python的增强结构”,并且可用于Python 2.x和Python3.x。
例如,在这种情况下,可以通过以下代码执行switch语句:
macro switch(arg1):
    while True:
        cont=False
        val=%arg1%
        socket case(arg2):
            if val==%arg2% or cont:
                cont=True
                socket
        socket else:
            socket
        break可以这样使用:
a=3
switch(a):
    case(0):
        print("Zero")
    case(1):
        print("Smaller than 2"):
        break
    else:
        print ("greater than 1")因此,espy在Python中将其翻译为:
a=3
while True:
    cont=False
    if a==0 or cont:
        cont=True
        print ("Zero")
    if a==1 or cont:
        cont=True
        print ("Smaller than 2")
        break
    print ("greater than 1")
    breakwhile True:生成的Python代码顶部的意义是什么?它将不可避免地命中break生成的Python代码的底部,因此在我看来,while True:和break都可以删除。此外,cont如果用户在自己的代码中使用相同的名称,ESPY是否足够聪明以更改名称?无论如何,我都想使用香草Python,所以我不会使用它,但是它仍然很酷。+1绝对酷。
                    while True:和breaks 的原因是允许但不要求掉线。
                    我发现了一个常见的开关结构:
switch ...parameter...
case p1: v1; break;
case p2: v2; break;
default: v3;可以用Python表示如下:
(lambda x: v1 if p1(x) else v2 if p2(x) else v3)或以更清晰的方式格式化:
(lambda x:
     v1 if p1(x) else
     v2 if p2(x) else
     v3)python版本不是表达式,而是表达式,其计算结果为值。
parameter以及p1==parameter
                    f = lambda x: 'a' if x==0 else 'b' if x==1 else 'c'。当我后来打电话时f(2),我得到了'c'; f(1),'b'; 并且f(0),'a'。至于p1(x),它表示一个谓词;只要它返回Trueor False,无论它是函数调用还是表达式,都可以。
                    这里的大多数答案都是很旧的,尤其是已被接受的答案,因此似乎值得更新。
首先,正式的Python FAQ涵盖了这一点,并elif为简单案例和dict较大或更复杂的案例推荐了链。它还visit_针对某些情况建议了一组方法(许多服务器框架使用的一种样式):
def dispatch(self, value):
    method_name = 'visit_' + str(value)
    method = getattr(self, method_name)
    method()常见问题解答中还提到了PEP 275,它是为了获得添加C样式转换语句的一劳永逸的正式决定而编写的。但是该PEP实际上推迟到了Python 3,并且只是作为单独的提案PEP 3103被正式拒绝。答案当然不是。但是,如果您对原因或历史记录感兴趣,则两个PEP都有指向其他信息的链接。
多次出现的一件事(即使在PEP 275中作为实际建议被删除,也可以在PEP 275中看到)是,如果您真的不愿意拥有8行代码来处理4种情况,而6条代码使用C或Bash编写的行,您始终可以这样编写:
if x == 1: print('first')
elif x == 2: print('second')
elif x == 3: print('third')
else: print('did not place')PEP 8并不完全鼓励这样做,但它可读性强,而且不太通用。
自从PEP 3103被拒绝以来的十多年里,C风格的案例声明,甚至Go中功能更强大的版本,都被认为已经死了。每当有人提出python-ideas或-dev时,都会参考旧的决定。
但是,完全ML样式模式匹配的想法每隔几年就会出现,特别是因为Swift和Rust这样的语言已经采用了它。问题在于,如果没有代数数据类型,很难从模式匹配中获得更多使用。尽管Guido对这个想法很同情,但是没有人提出一个非常适合Python的提议。(您可以阅读我的2014年草民的示例。)这可能会dataclass在3.7版本中发生变化,并偶尔会提出一些更强大的建议enum以处理总和类型,或者针对各种类型的语句局部绑定(例如PEP 3150)提出各种建议或目前正在-ideas上讨论的一组建议)。但是到目前为止,还没有。
偶尔也有关于Perl 6样式匹配的建议,这基本上是从elif正则表达式到单分派类型切换的所有内容的混搭。
运行功能的解决方案:
result = {
    'case1':     foo1, 
    'case2':     foo2,
    'case3':     foo3,
    'default':   default,
}.get(option)()其中foo1(),foo2(),foo3()和default()是函数
foo2()的foo1(),foo3()和default()功能都也将运行,这意味着事情可能需要很长的时间
                    get(option)()。问题解决了。
                    在Google搜索的任何地方都找不到我想要的简单答案。但是我还是想通了。这真的很简单。决定将其张贴,并可能防止在其他人的头部上刮一些划痕。关键只是“输入”和元组。这是带有穿透的switch语句行为,包括RANDOM穿透。
l = ['Dog', 'Cat', 'Bird', 'Bigfoot',
     'Dragonfly', 'Snake', 'Bat', 'Loch Ness Monster']
for x in l:
    if x in ('Dog', 'Cat'):
        x += " has four legs"
    elif x in ('Bat', 'Bird', 'Dragonfly'):
        x += " has wings."
    elif x in ('Snake',):
        x += " has a forked tongue."
    else:
        x += " is a big mystery by default."
    print(x)
print()
for x in range(10):
    if x in (0, 1):
        x = "Values 0 and 1 caught here."
    elif x in (2,):
        x = "Value 2 caught here."
    elif x in (3, 7, 8):
        x = "Values 3, 7, 8 caught here."
    elif x in (4, 6):
        x = "Values 4 and 6 caught here"
    else:
        x = "Values 5 and 9 caught in default."
    print(x)提供:
Dog has four legs
Cat has four legs
Bird has wings.
Bigfoot is a big mystery by default.
Dragonfly has wings.
Snake has a forked tongue.
Bat has wings.
Loch Ness Monster is a big mystery by default.
Values 0 and 1 caught here.
Values 0 and 1 caught here.
Value 2 caught here.
Values 3, 7, 8 caught here.
Values 4 and 6 caught here
Values 5 and 9 caught in default.
Values 4 and 6 caught here
Values 3, 7, 8 caught here.
Values 3, 7, 8 caught here.
Values 5 and 9 caught in default.break在代码末尾写一个case。但是我认为我们不需要这样的 “
                    我使用的解决方案:
此处发布的两种解决方案的组合,相对易于阅读并支持默认设置。
result = {
  'a': lambda x: x * 5,
  'b': lambda x: x + 7,
  'c': lambda x: x - 2
}.get(whatToUse, lambda x: x - 22)(value)哪里
.get('c', lambda x: x - 22)(23)"lambda x: x - 2"在字典中查找并与x=23 
.get('xxx', lambda x: x - 22)(44)不会在字典中找到它,而是将默认值"lambda x: x - 22"与一起使用x=44。
def f(x):
    dictionary = {'a':1, 'b':2, 'c':3}
    return dictionary.get(x,'Not Found') 
##Returns the value for the letter x;returns 'Not Found' if x isn't a key in the dictionary我喜欢Mark Bies的回答
由于x变量必须使用两次,因此我将lambda函数修改为无参数。
我必须与  results[value](value)
In [2]: result = {
    ...:   'a': lambda x: 'A',
    ...:   'b': lambda x: 'B',
    ...:   'c': lambda x: 'C'
    ...: }
    ...: result['a']('a')
    ...: 
Out[2]: 'A'
In [3]: result = {
    ...:   'a': lambda : 'A',
    ...:   'b': lambda : 'B',
    ...:   'c': lambda : 'C',
    ...:   None: lambda : 'Nothing else matters'
    ...: }
    ...: result['a']()
    ...: 
Out[3]: 'A'编辑:我注意到我可以将Nonetype与字典配合使用。所以这会效仿switch ; case else
result[None]()吗?
                    result = {'a': 100, None:5000}; result[None]
                    None:行为就好default:。
                    您可以使用调度的字典:
#!/usr/bin/env python
def case1():
    print("This is case 1")
def case2():
    print("This is case 2")
def case3():
    print("This is case 3")
token_dict = {
    "case1" : case1,
    "case2" : case2,
    "case3" : case3,
}
def main():
    cases = ("case1", "case3", "case2", "case1")
    for case in cases:
        token_dict[case]()
if __name__ == '__main__':
    main()输出:
This is case 1
This is case 3
This is case 2
This is case 1简单,未经测试;每种条件都是独立评估的:没有穿透,但是所有情况都进行评估(尽管打开的表达式仅评估一次),除非有break语句。例如,
for case in [expression]:
    if case == 1:
        print(end='Was 1. ')
    if case == 2:
        print(end='Was 2. ')
        break
    if case in (1, 2):
        print(end='Was 1 or 2. ')
    print(end='Was something. ')打印Was 1. Was 1 or 2. Was something. (该死!为什么我不能有拖尾的内嵌代码块空白?)如果expression计算结果为1,Was 2.如果expression计算结果为2,或者Was something.如果expression计算结果为别的东西。
定义:
def switch1(value, options):
  if value in options:
    options[value]()允许您使用相当简单的语法,将案例捆绑到地图中:
def sample1(x):
  local = 'betty'
  switch1(x, {
    'a': lambda: print("hello"),
    'b': lambda: (
      print("goodbye," + local),
      print("!")),
    })我一直试图以一种让我摆脱“ lambda:”的方式重新定义开关,但是放弃了。调整定义:
def switch(value, *maps):
  options = {}
  for m in maps:
    options.update(m)
  if value in options:
    options[value]()
  elif None in options:
    options[None]()允许我将多个案例映射到同一代码,并提供默认选项:
def sample(x):
  switch(x, {
    _: lambda: print("other") 
    for _ in 'cdef'
    }, {
    'a': lambda: print("hello"),
    'b': lambda: (
      print("goodbye,"),
      print("!")),
    None: lambda: print("I dunno")
    })每个重复的案例都必须放在自己的字典中;在查找值之前,switch()合并字典。它仍然比我想要的还要难看,但是它具有在表达式上使用哈希查找的基本效率,而不是循环遍历所有键。
我认为最好的方法是使用python语言惯用语来保持代码可测试。如之前的答案所示,我使用字典来利用python结构和语言并以不同的方法隔离“案例”代码。下面有一个类,但是您可以直接使用模块,全局变量和函数。该类具有可以隔离测试的方法。根据您的需求,您也可以使用静态方法和属性。
class ChoiceManager:
    def __init__(self):
        self.__choice_table = \
        {
            "CHOICE1" : self.my_func1,
            "CHOICE2" : self.my_func2,
        }
    def my_func1(self, data):
        pass
    def my_func2(self, data):
        pass
    def process(self, case, data):
        return self.__choice_table[case](data)
ChoiceManager().process("CHOICE1", my_data)也可以通过将类用作 “ __choice_table”的键来利用此方法。这样,您可以避免实例滥用,并保持所有清洁和可测试的状态。
假设您必须处理来自网络或MQ的大量消息或数据包。每个数据包都有自己的结构和管理代码(以通用方式)。使用上面的代码,可以执行以下操作:
class PacketManager:
    def __init__(self):
        self.__choice_table = \
        {
            ControlMessage : self.my_func1,
            DiagnosticMessage : self.my_func2,
        }
    def my_func1(self, data):
        # process the control message here
        pass
    def my_func2(self, data):
        # process the diagnostic message here
        pass
    def process(self, pkt):
        return self.__choice_table[pkt.__class__](pkt)
pkt = GetMyPacketFromNet()
PacketManager().process(pkt)
# isolated test or isolated usage example
def test_control_packet():
    p = ControlMessage()
    PacketManager().my_func1(p)因此,复杂性不会在代码流中扩散,而是以代码结构呈现。
扩展Greg Hewgill的答案 -我们可以使用装饰器封装字典解决方案:
def case(callable):
    """switch-case decorator"""
    class case_class(object):
        def __init__(self, *args, **kwargs):
            self.args = args
            self.kwargs = kwargs
        def do_call(self):
            return callable(*self.args, **self.kwargs)
return case_class
def switch(key, cases, default=None):
    """switch-statement"""
    ret = None
    try:
        ret = case[key].do_call()
    except KeyError:
        if default:
            ret = default.do_call()
    finally:
        return ret然后可以与@case-decorator 一起使用
@case
def case_1(arg1):
    print 'case_1: ', arg1
@case
def case_2(arg1, arg2):
    print 'case_2'
    return arg1, arg2
@case
def default_case(arg1, arg2, arg3):
    print 'default_case: ', arg1, arg2, arg3
ret = switch(somearg, {
    1: case_1('somestring'),
    2: case_2(13, 42)
}, default_case(123, 'astring', 3.14))
print ret好消息是,这已经在NeoPySwitch -module中完成。只需使用pip安装:
pip install NeoPySwitch我倾向于使用也使用字典的解决方案是:
def decision_time( key, *args, **kwargs):
    def action1()
        """This function is a closure - and has access to all the arguments"""
        pass
    def action2()
        """This function is a closure - and has access to all the arguments"""
        pass
    def action3()
        """This function is a closure - and has access to all the arguments"""
        pass
   return {1:action1, 2:action2, 3:action3}.get(key,default)()这样做的好处是,它不必每次都尝试评估函数,而只需确保外部函数获取内部函数所需的所有信息即可。
到目前为止,已经有很多答案说:“我们没有在Python中进行切换,可以这样做”。但是,我想指出的是,switch语句本身是一个易于滥用的构造,在大多数情况下可以并且应该避免使用它们,因为它们会促进惰性编程。例子:
def ToUpper(lcChar):
    if (lcChar == 'a' or lcChar == 'A'):
        return 'A'
    elif (lcChar == 'b' or lcChar == 'B'):
        return 'B'
    ...
    elif (lcChar == 'z' or lcChar == 'Z'):
        return 'Z'
    else:
        return None        # or something现在,您可以使用切换语句来执行此操作(如果Python提供了一个语句),但是您会浪费时间,因为有些方法可以很好地完成此操作。或者,也许您不那么明显:
def ConvertToReason(code):
    if (code == 200):
        return 'Okay'
    elif (code == 400):
        return 'Bad Request'
    elif (code == 404):
        return 'Not Found'
    else:
        return None但是,这种操作可以并且应该用字典来处理,因为它将更快,更简单,更不容易出错并且更紧凑。
switch语句的绝大多数“用例”将属于这两种情况之一;如果您已经彻底考虑了问题,则几乎没有理由使用它。
因此,与其问“如何切换Python?”,不如问“为什么要切换Python”?因为这通常是更有趣的问题,并且经常会暴露出您要构建的产品的设计缺陷。
现在,这并不是说也不应该使用任何开关。状态机,词法分析器,解析器和自动机在某种程度上都使用它们,通常,当您从对称输入开始并转到非对称输出时,它们可能会有用。您只需要确保不要将开关用作锤子,因为在代码中会看到很多钉子。