V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · 6896 views
    This topic created in 2942 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
        1
    changrui0608  
       Apr 17, 2018   ❤️ 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  
       Apr 17, 2018 via Android
    一楼正解
    super452
        3
    super452  
       Apr 17, 2018
    学习了
    LaFayette
        4
    LaFayette  
       Apr 17, 2018
    用字典啊, 哥
    wsds
        5
    wsds  
    OP
       Apr 17, 2018
    @qianc1990 if 条件包括多个判断语句啊,用字典怎么搞?
    if a==b and a>0:
    condition
    L2AKnG8GXx60bc6P
        6
    L2AKnG8GXx60bc6P  
       Apr 17, 2018
    如果对顺序有要求,不要用字典,用 tuple 封装
    wsds
        7
    wsds  
    OP
       Apr 17, 2018
    @changrui0608 我比较好奇 lambda 表达式
    SuperMild
        8
    SuperMild  
       Apr 17, 2018
    其实直接用多个 if elif 就很好。
    wsds
        9
    wsds  
    OP
       Apr 17, 2018
    @relic 老铁,借一步说话
    Hopetree
        10
    Hopetree  
       Apr 17, 2018
    不明白为什么要把这种最简单明了且容易读的代码改成别的?
    写代码的目的难道不是为了在不影响效果的前提下简单明了易读吗,if elif 最完美
    quickma
        11
    quickma  
       Apr 17, 2018   ❤️ 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  
       Apr 17, 2018
    @x7395759 厉害了,没想到官方居然还真考虑过这个问题:smile:
    xieranmaya
        13
    xieranmaya  
       Apr 17, 2018
    如果我没记错的话,是专门不设计 switch 语句的
    testsec
        14
    testsec  
       Apr 17, 2018 via iPhone
    又学到一波操作
    gnozix
        15
    gnozix  
       Apr 18, 2018
    Python cookbook 你值得拥有
    alvy
        16
    alvy  
       Apr 18, 2018
    字典
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1078 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 89ms · UTC 23:00 · PVG 07:00 · LAX 16:00 · JFK 19:00
    ♥ Do have faith in what you're doing.