与我之前使用的所有语言都不同,Python没有switch/case语句。为了达到这种分支语句的效果,一般方法是使用字典映射:

def numbers_to_strings(argument):
    switcher = {
        0: "zero",
        1: "one",
        2: "two",
    }
    return switcher.get(argument, "nothing")

这段代码的作用相当于:

function(argument){
    switch(argument) {
        case 0:
            return "zero";
        case 1:
            return "one";
        case 2:
            return "two";
        default:
            return "nothing";
    };
};

看起来,Python代码的对分支情况的处理方式比switch语句要更加简洁,但是我也可以认为它更晦涩难懂。我刚开始写Python代码的时候,总觉得怪怪的。后来时间长了,使用字典key作为分支条件,就越来越得心应手,越来越习惯了。

 

字典映射到函数

在Python中,字典可以映射到函数或则lambda表达式

def zero():
    return "zero"

def one():
    return "one"

def numbers_to_functions_to_strings(argument):
    switcher = {
        0: zero,
        1: one,
        2: lambda: "two",
    }
    # Get the function from switcher dictionary
    func = switcher.get(argument, lambda: "nothing")
    # Execute the function
    return func()

虽然上例中zero()和one()中的代码非常简单,但是许多Python程序都用字典映射来分配处理复杂的程序流程。

 

指派到类方法

在一个类里,如果我们不知道该调用哪个方法,那么我们可以使用一个分派方法在运行时决定:

class Switcher(object):
    def numbers_to_methods_to_strings(self, argument):
        """Dispatch method"""
        # prefix the method_name with 'number_' because method names
        # cannot begin with an integer.
        method_name = 'number_' + str(argument)
        # Get the method from 'self'. Default to a lambda.
        method = getattr(self, method_name, lambda: "nothing")
        # Call the method as we return it
        return method()

    def number_0(self):
        return "zero"

    def number_1(self):
        return "one"

    def number_2(self):
        return "two"

漂亮的实现,对吧?

 

官方解释

官方说法是,“你可以用一系列的 if...elif...elif...else 语句来处理这些问题”。而且还能使用字典映射到函数,分派到类方法。

令人疑惑的是,官方只给出了替代方案,而并没有解释为什么。换句话说,就是“无可奉告“。在我看来,官方想要表达的意思其实就是”Python不需要case语句“。

 

真相是什么?

然而我听得最多的说法是,switch/case语句非常难以调试。

但是稍微思考一下就知道这种说法是站不住脚的。只需要假想一下,你使用了一个重重嵌套的巨大字典用来处理分支逻辑;如果这个字典元素超过了100个,那么它的调试难度其实并不低于100个case语句。

 

或许因为字典映射速度更快?

然并卵,Python没有switch/case语句,没法测试,跳过这一点。

 

Python这种做法的巨大优势

经验之谈,我经常会碰到一些情景,Python的做法比switch/case语句要优雅有效的多,那就是在我需要在运行时增删映射项的时候。碰到需要这么做的时候,我的Python技能就碉堡了,可以动态的改变字典映射和类方法分派调用。有了这些心得之后,我在也没有怀念过switch/case语句。

 

最终章

对我而言,使用Python的经验迫使我使用字典映射,而我亦从中因祸得福。没有switch/case语句的苦恼使得我产生了以前没有过的想法、实现了以前没开发过的功能。

总而言之,Python switch/case语句的缺失,使我成为了更好的程序员;而这种开发生态,就是我所期望的比“官方解释”更好的答案。

翻译自http://www.pydanny.com/why-doesnt-python-have-switch-case.html