@@ -258,9 +258,11 @@ is evaluated in all cases.
258
258
Why isn't there a switch or case statement in Python?
259
259
-----------------------------------------------------
260
260
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 ``.
264
266
265
267
For cases where you need to choose from a very large number of possibilities,
266
268
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
289
291
this example. Without such a prefix, if values are coming from an untrusted
290
292
source, an attacker would be able to call any method on your object.
291
293
294
+ Imitating switch with fallthrough, as with C's switch-case-default,
295
+ is possible, much harder, and less needed.
296
+
292
297
293
298
Can't you emulate threads in the interpreter instead of relying on an OS-specific thread implementation?
294
299
--------------------------------------------------------------------------------------------------------
0 commit comments