前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python switch/case语句

Python switch/case语句

作者头像
py3study
发布2020-01-07 20:17:51
8.6K0
发布2020-01-07 20:17:51
举报
文章被收录于专栏:python3python3

与Java、C\C++等语言不同,Python中是不提供switch/case语句的,这一点让我感觉到很奇怪。我们可以通过如下几种方法来实现switch/case语句。

使用if…elif…elif…else 实现switch/case

可以使用if…elif…elif..else序列来代替switch/case语句,这是大家最容易想到的办法。但是随着分支的增多和修改的频繁,这种代替方式并不很好调试和维护。

使用字典 实现switch/case

可以使用字典实现switch/case这种方式易维护,同时也能够减少代码量。如下是使用字典模拟的switch/case实现:

代码语言:javascript
复制
def num_to_string(num):
    numbers = {
        0 : "zero",
        1 : "one",
        2 : "two",
        3 : "three"
    }

    return numbers.get(num, None)

if __name__ == "__main__":
    print num_to_string(2)
    print num_to_string(5)

执行结果如下:

代码语言:javascript
复制
two
None

Python字典中还可以包括函数或Lambda表达式,代码如下:

代码语言:javascript
复制
def success(msg):
    print msg

def debug(msg):
    print msg

def error(msg):
    print msg

def warning(msg):
    print msg

def other(msg):
    print msg

def notify_result(num, msg):
    numbers = {
        0 : success,
        1 : debug,
        2 : warning,
        3 : error
    }

    method = numbers.get(num, other)
    if method:
        method(msg)

if __name__ == "__main__":
    notify_result(0, "success")
    notify_result(1, "debug")
    notify_result(2, "warning")
    notify_result(3, "error")
    notify_result(4, "other")

执行结果如下:

代码语言:javascript
复制
success
debug
warning
error
other

通过如上示例可以证明能够通过Python字典来完全实现switch/case语句,而且足够灵活。尤其在运行时可以很方便的在字典中添加或删除一个switch/case选项。

在类中可使用调度方法实现switch/case

如果在一个类中,不确定要使用哪种方法,可以用一个调度方法在运行的时候来确定。代码如下:

代码语言:javascript
复制
class switch_case(object):

    def case_to_function(self, case):
        fun_name = "case_fun_" + str(case)
        method = getattr(self, fun_name, self.case_fun_other)
        return method

    def case_fun_1(self, msg):
        print msg

    def case_fun_2(self, msg):
        print msg

    def case_fun_other(self, msg):
        print msg


if __name__ == "__main__":
    cls = switch_case()
    cls.case_to_function(1)("case_fun_1")
    cls.case_to_function(2)("case_fun_2")
    cls.case_to_function(3)("case_fun_other")

执行结果如下:

代码语言:javascript
复制
case_fun_1
case_fun_2
case_fun_other

总结

就个人来说,使用字典来实现switch/case是最为灵活的,但是理解上也有一定的难度。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用if…elif…elif…else 实现switch/case
  • 使用字典 实现switch/case
  • 在类中可使用调度方法实现switch/case
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档