Skip to content

Commit 1dcb461

Browse files
[3.11] gh-111307: Update design FAQ 'switch' entry (GH-115899) (#116704)
(cherry picked from commit 43986f5) Co-authored-by: Terry Jan Reedy <[email protected]>
1 parent 4a73637 commit 1dcb461

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
@@ -258,9 +258,11 @@ is evaluated in all cases.
258258
Why isn't there a switch or case statement in Python?
259259
-----------------------------------------------------
260260

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

265267
For cases where you need to choose from a very large number of possibilities,
266268
you can create a dictionary mapping case values to functions to call. For
@@ -289,6 +291,9 @@ It's suggested that you use a prefix for the method names, such as ``visit_`` in
289291
this example. Without such a prefix, if values are coming from an untrusted
290292
source, an attacker would be able to call any method on your object.
291293

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

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

0 commit comments

Comments
 (0)