V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
wsds
V2EX  ›  Python

Python 没有 switch case,于是写了 n 多个 if else,有简写的方法吗?

  •  
  •   wsds · 2018-04-17 13:01:49 +08:00 · 5906 次点击
    这是一个创建于 2172 天前的主题,其中的信息可能已经有所发展或是发生改变。

    condition 条件语句包含多个判断条件的话

    if condition_1:
        statement_block_1
    elif condition_2:
        statement_block_2
    elif condition_3:
        statement_block_3
    elif condition_4:
        statement_block_4
    elif condition_5:
        statement_block_6
    else:
        statement_block_7
    
               
    
    16 条回复    2018-04-18 10:29:00 +08:00
    changrui0608
        1
    changrui0608  
       2018-04-17 13:11:20 +08:00   ❤️ 2
    不确定是不是足够好的方法,如果分支太多可以用字典存 func,或者可以用 default dict 应对 default 情况
    ```py
    def func1(*args, **kwargs):
    pass

    def func2(*args, **kwargs):
    pass

    func_dict = {
    'condition1': func1,
    'condition2': func2,
    }

    real_condition = 'condition1'
    func_dict[real_condition]()
    ```

    逻辑特别简单的话,也可以用 lambda 表达式省行数
    wnma3mz
        2
    wnma3mz  
       2018-04-17 13:41:15 +08:00 via Android
    一楼正解
    super452
        3
    super452  
       2018-04-17 13:43:59 +08:00
    学习了
    qianc1990
        4
    qianc1990  
       2018-04-17 13:47:03 +08:00
    用字典啊, 哥
    wsds
        5
    wsds  
    OP
       2018-04-17 13:51:07 +08:00
    @qianc1990 if 条件包括多个判断语句啊,用字典怎么搞?
    if a==b and a>0:
    condition
    relic
        6
    relic  
       2018-04-17 13:51:16 +08:00
    如果对顺序有要求,不要用字典,用 tuple 封装
    wsds
        7
    wsds  
    OP
       2018-04-17 13:53:04 +08:00
    @changrui0608 我比较好奇 lambda 表达式
    SuperMild
        8
    SuperMild  
       2018-04-17 13:55:06 +08:00
    其实直接用多个 if elif 就很好。
    wsds
        9
    wsds  
    OP
       2018-04-17 14:03:20 +08:00
    @relic 老铁,借一步说话
    Hopetree
        10
    Hopetree  
       2018-04-17 15:40:32 +08:00
    不明白为什么要把这种最简单明了且容易读的代码改成别的?
    写代码的目的难道不是为了在不影响效果的前提下简单明了易读吗,if elif 最完美
    x7395759
        11
    x7395759  
       2018-04-17 16:00:13 +08:00   ❤️ 1
    @Hopetree 效率上有差距,虽然大多数的情况下不管,但是作为纯技术讨论是有意义的

    附带官方解释: https://docs.python.org/2/faq/design.html#why-isn-t-there-a-switch-or-case-statement-in-python

    if else 是官方认可的方式
    Hopetree
        12
    Hopetree  
       2018-04-17 16:43:06 +08:00
    @x7395759 厉害了,没想到官方居然还真考虑过这个问题:smile:
    xieranmaya
        13
    xieranmaya  
       2018-04-17 17:03:02 +08:00
    如果我没记错的话,是专门不设计 switch 语句的
    testsec
        14
    testsec  
       2018-04-17 22:28:47 +08:00 via iPhone
    又学到一波操作
    gnozix
        15
    gnozix  
       2018-04-18 09:26:54 +08:00
    Python cookbook 你值得拥有
    alvy
        16
    alvy  
       2018-04-18 10:29:00 +08:00
    字典
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2691 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 15:45 · PVG 23:45 · LAX 08:45 · JFK 11:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.