Repository navigation
Update design FAQ 'switch' entry mention of match-case. #111307
Description
Activity
Sounds like a good idea to recommend
matchat 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?
- added3.11only security fixesonly security fixes3.12only security fixesonly security fixes3.13only security fixesonly security fixes
on Oct 25, 2023 I suppose it is in https://github.com/python/cpython/blob/main/Doc/faq/design.rst ? I try
- added a commit that references this issue
on Oct 25, 2023 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.- changed the title
[-]Update design page about `match` keyword[/-][+]Update design FAG 'switch' entry mention of `match-case`.[/+]on Feb 25, 2024 - 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 - added a commit that references this issue
on Feb 25, 2024 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 caseisn't cited", the entry says in the 2nd sentence, "For literal values, or constants within a namespace, you can also use amatch ... casestatement." 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.- added a commit that references this issue
on Mar 13, 2024
Documentation
Describe the bug
I did a search to remember what was the
switchkeyword in Python, and the very first link I got is a not up-to-date page from Python 3.11 documentation, explaining thatelifis great.To Reproduce
Steps to reproduce the behavior:
match caseisn't cited, but the catastrophicelifcascade is still the “reference”Expected behavior
Correct it and explain why
match caseis a really better coding practice instead of cascadedelif(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 replaceelifwith simplierif!Linked PRs