diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2024-03-13 05:36:43 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-13 05:36:43 (GMT) |
commit | d0c32ae419316cb0d6f06ec8cb2f6b91a878070f (patch) | |
tree | 8eb4c34b7659746a02d6ae45b2e002d723adf081 /Doc/faq | |
parent | 982ff4a4b7282a104bf7827282698a1e39983c65 (diff) | |
download | cpython-d0c32ae419316cb0d6f06ec8cb2f6b91a878070f.zip cpython-d0c32ae419316cb0d6f06ec8cb2f6b91a878070f.tar.gz cpython-d0c32ae419316cb0d6f06ec8cb2f6b91a878070f.tar.bz2 |
[3.12] gh-111307: Update design FAQ 'switch' entry (GH-115899) (#116703)
(cherry picked from commit 43986f55671ba2f7b08f8c5cea69aa136a093697)
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
Diffstat (limited to 'Doc/faq')
-rw-r--r-- | Doc/faq/design.rst | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst index 300e1b6..c8beb64 100644 --- a/Doc/faq/design.rst +++ b/Doc/faq/design.rst @@ -259,9 +259,11 @@ is evaluated in all cases. Why isn't there a switch or case statement in Python? ----------------------------------------------------- -You can do this easily enough with a sequence of ``if... elif... elif... else``. -For literal values, or constants within a namespace, you can also use a -``match ... case`` statement. +In general, structured switch statements execute one block of code +when an expression has a particular value or set of values. +Since Python 3.10 one can easily match literal values, or constants +within a namespace, with a ``match ... case`` statement. +An older alternative is a sequence of ``if... elif... elif... else``. For cases where you need to choose from a very large number of possibilities, you can create a dictionary mapping case values to functions to call. For @@ -290,6 +292,9 @@ It's suggested that you use a prefix for the method names, such as ``visit_`` in this example. Without such a prefix, if values are coming from an untrusted source, an attacker would be able to call any method on your object. +Imitating switch with fallthrough, as with C's switch-case-default, +is possible, much harder, and less needed. + Can't you emulate threads in the interpreter instead of relying on an OS-specific thread implementation? -------------------------------------------------------------------------------------------------------- |