From 0071bdb25a0edcab946e7664b89e02c99931ff4c Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Sun, 25 Feb 2024 02:49:41 -0500 Subject: [PATCH 1/2] gh-111307: Update design FAQ 'switch' entry --- Doc/faq/design.rst | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst index 300e1b6cc40a58..fd36dfe5830c5f 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 Fortran's computed GOTO +and 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? -------------------------------------------------------------------------------------------------------- From 7aada4ea70ced74667553d64f3970ef12b4f7af9 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Tue, 12 Mar 2024 07:26:06 -0400 Subject: [PATCH 2/2] Eliminate Fortran reference --- Doc/faq/design.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst index fd36dfe5830c5f..c8beb64e39bc1a 100644 --- a/Doc/faq/design.rst +++ b/Doc/faq/design.rst @@ -292,8 +292,8 @@ 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 Fortran's computed GOTO -and C's switch-case-default, is possible, much harder, and less needed. +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?