Skip to content

Commit d0c32ae

Browse files
[3.12] gh-111307: Update design FAQ 'switch' entry (GH-115899) (#116703)
(cherry picked from commit 43986f5) Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
1 parent 982ff4a commit d0c32ae

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

Doc/faq/design.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,11 @@ is evaluated in all cases.
259259
Why isn't there a switch or case statement in Python?
260260
-----------------------------------------------------
261261

262-
You can do this easily enough with a sequence of ``if... elif... elif... else``.
263-
For literal values, or constants within a namespace, you can also use a
264-
``match ... case`` statement.
262+
In general, structured switch statements execute one block of code
263+
when an expression has a particular value or set of values.
264+
Since Python 3.10 one can easily match literal values, or constants
265+
within a namespace, with a ``match ... case`` statement.
266+
An older alternative is a sequence of ``if... elif... elif... else``.
265267

266268
For cases where you need to choose from a very large number of possibilities,
267269
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
290292
this example. Without such a prefix, if values are coming from an untrusted
291293
source, an attacker would be able to call any method on your object.
292294

295+
Imitating switch with fallthrough, as with C's switch-case-default,
296+
is possible, much harder, and less needed.
297+
293298

294299
Can't you emulate threads in the interpreter instead of relying on an OS-specific thread implementation?
295300
--------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy