Skip to content

Update design FAQ 'switch' entry mention of match-case. #111307

Description

@dascritch

Documentation

Describe the bug
I did a search to remember what was the switch keyword in Python, and the very first link I got is a not up-to-date page from Python 3.11 documentation, explaining that elif is great.

To Reproduce
Steps to reproduce the behavior:

  1. Go to https://docs.python.org/3.12/faq/design.html
  2. Click on Why isn’t there a switch or case statement in Python?
  3. match case isn't cited, but the catastrophic elif cascade is still the “reference”

Expected behavior
Correct it and explain why match case is a really better coding practice instead of cascaded elif (unreadable, unmaintainable, confuse)

Sidenote :
Found here what I was looking at.

Even on this not related page the author use return, and didn't think to replace elif with simplier if !

Linked PRs

Activity

  1. hugovk commented on Oct 25, 2023

    @hugovk
    Member

    Sounds like a good idea to recommend match at https://docs.python.org/3/faq/design.html?highlight=switch#why-isn-t-there-a-switch-or-case-statement-in-python, mentioning it's new in 3.10.

    Would you like to propose something in a PR?

  2. added
    3.11only security fixes
    3.12only security fixes
    3.13only security fixes
    on Oct 25, 2023
  3. dascritch commented on Oct 25, 2023

    @dascritch
    Author
  4. mdboom commented on Oct 25, 2023

    @mdboom
    Contributor
  5. terryjreedy commented on Feb 25, 2024

    @terryjreedy
    Member

    The diatribe against if-elif-else is wrong-headed. Match-case statements are executed as if case matches, do action, elif next case matches, do action, .... else _, do last action. People must understand this to read and write such properly. If-else is better for value range matching, which is quite common, since one otherwise has to add an if-clause to case statements anyway.

    I write if-elif-else with return rather than if-if-if return since it is clearer at a glance that case-analysis is being done and exactly one condition will match and one suite executed. In simple cases, such can be reduced to return true_val if condition else false_val.

  6. changed the title [-]Update design page about `match` keyword[/-] [+]Update design FAG 'switch' entry mention of `match-case`.[/+] on Feb 25, 2024
  7. changed the title [-]Update design FAG 'switch' entry mention of `match-case`.[/-] [+]Update design FAQ 'switch' entry mention of `match-case`.[/+] on Feb 25, 2024
  8. added a commit that references this issue on Feb 25, 2024
  9. terryjreedy commented on Feb 25, 2024

    @terryjreedy
    Member

    The existing doc, at the time of the PR, does not say the 'elif is great'. I does say that that one "can do this easily enough", which I consider true. Contrary to "match case isn't cited", the entry says in the 2nd sentence, "For literal values, or constants within a namespace, you can also use a match ... case statement." So I do not see any urgent need for revision, but I do see room for improvement. PR #115899 mentions match-case first as Python's new switch-case statement and de-emphasizes if-else.

  10. added a commit that references this issue on Mar 13, 2024
  11. added 2 commits that reference this issue on Mar 13, 2024
  12. added 2 commits that reference this issue on Mar 13, 2024
  13. added a commit that references this issue on Mar 20, 2024
  14. added a commit that references this issue on Mar 25, 2024
  15. added a commit that references this issue on Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only security fixes3.12only security fixes3.13only security fixesdocsDocumentation in the Doc dir

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions