推荐学习书目
› 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 · Apr 17, 2018 · 7461 views
    This topic created in 3124 days ago, the information mentioned may be changed or developed.

    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 replies  •  2018-04-18 10:29:00 +08:00
    changrui0608
    changrui0608
    Apr 17, 2018❤️2
    1
    不确定是不是足够好的方法,如果分支太多可以用字典存 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
    wnma3mz
    Apr 17, 2018 via Android
    2
    一楼正解
    super452
    super452
    Apr 17, 2018
    3
    学习了
    LaFayette
    LaFayette
    Apr 17, 2018
    4
    用字典啊, 哥
    wsds
    wsds
    OP
    Apr 17, 2018
    5
    @qianc1990 if 条件包括多个判断语句啊,用字典怎么搞?
    if a==b and a>0:
    condition
    L2AKnG8GXx60bc6P
    L2AKnG8GXx60bc6P
    Apr 17, 2018
    6
    如果对顺序有要求,不要用字典,用 tuple 封装
    wsds
    wsds
    OP
    Apr 17, 2018
    7
    @changrui0608 我比较好奇 lambda 表达式
    SuperMild
    SuperMild
    Apr 17, 2018
    8
    其实直接用多个 if elif 就很好。
    wsds
    wsds
    OP
    Apr 17, 2018
    9
    @relic 老铁,借一步说话
    Hopetree
    Hopetree
    Apr 17, 2018
    10
    不明白为什么要把这种最简单明了且容易读的代码改成别的?
    写代码的目的难道不是为了在不影响效果的前提下简单明了易读吗,if elif 最完美
    quickma
    quickma
    Apr 17, 2018❤️1
    11
    @Hopetree 效率上有差距,虽然大多数的情况下不管,但是作为纯技术讨论是有意义的

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

    if else 是官方认可的方式
    Hopetree
    Hopetree
    Apr 17, 2018
    12
    @x7395759 厉害了,没想到官方居然还真考虑过这个问题:smile:
    xieranmaya
    xieranmaya
    Apr 17, 2018
    13
    如果我没记错的话,是专门不设计 switch 语句的
    testsec
    testsec
    Apr 17, 2018 via iPhone
    14
    又学到一波操作
    gnozix
    gnozix
    Apr 18, 2018
    15
    Python cookbook 你值得拥有
    alvy
    alvy
    Apr 18, 2018
    16
    字典
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1767 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 51ms · UTC 16:59 · PVG 00:59 · LAX 08:59 · JFK 11:59
    ♥ Do have faith in what you're doing.