From d377b8c75b63b93c44fe04f30291d7565d7aef81 Mon Sep 17 00:00:00 2001 From: xzkdeng Date: Tue, 6 May 2025 20:31:13 +0800 Subject: [PATCH 1/7] Add example for the match statement Add example of the ``match`` statement in the FAQ "Why isn't there a switch or case statement in Python?", and links to PEP 634,PEP 636 for details. --- Doc/faq/design.rst | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst index e2710fab9cf800..c17a346a91eae2 100644 --- a/Doc/faq/design.rst +++ b/Doc/faq/design.rst @@ -262,7 +262,21 @@ Why isn't there a switch or case statement in Python? 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. +within a namespace, with a ``match ... case`` statement,like this:: + + command="a" + match command: + case "a": + function_1() + case "b": + function_2() + case "c": + self.method_1() + case _: # The "_" is a wildcard here. + print("invaild command") + +See :pep:`634` (specification) and :pep:`636` (tutorial) for details about the ``match`` 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, From 08674f43c9060b51177dfe13487e309fccd44562 Mon Sep 17 00:00:00 2001 From: xzkdeng Date: Tue, 6 May 2025 21:08:38 +0800 Subject: [PATCH 2/7] Update Doc/faq/design.rst,by suggestion of sobolevn -1 Co-authored-by: sobolevn --- Doc/faq/design.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst index c17a346a91eae2..acd1c10118cff6 100644 --- a/Doc/faq/design.rst +++ b/Doc/faq/design.rst @@ -262,7 +262,7 @@ Why isn't there a switch or case statement in Python? 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,like this:: +within a namespace, with a ``match ... case`` statement, like this:: command="a" match command: From 0fe18431063b0cf181842374463b0b3697994288 Mon Sep 17 00:00:00 2001 From: xzkdeng Date: Tue, 6 May 2025 21:09:17 +0800 Subject: [PATCH 3/7] Update Doc/faq/design.rst,by suggestion of sobolevn -2 Co-authored-by: sobolevn --- Doc/faq/design.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst index acd1c10118cff6..1cfa34297b5708 100644 --- a/Doc/faq/design.rst +++ b/Doc/faq/design.rst @@ -275,7 +275,7 @@ within a namespace, with a ``match ... case`` statement, like this:: case _: # The "_" is a wildcard here. print("invaild command") -See :pep:`634` (specification) and :pep:`636` (tutorial) for details about the ``match`` statement. +See :pep:`634` (specification) and :pep:`636` (tutorial) for details about the ``match`` statement. An older alternative is a sequence of ``if... elif... elif... else``. From eb548179f9858399082bc1ef399c51d1fc48c72a Mon Sep 17 00:00:00 2001 From: xzkdeng Date: Tue, 6 May 2025 21:18:17 +0800 Subject: [PATCH 4/7] Update Doc/faq/design.rst,by suggestion of sobolevn -3 --- Doc/faq/design.rst | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst index 1cfa34297b5708..99b67a065ab7a3 100644 --- a/Doc/faq/design.rst +++ b/Doc/faq/design.rst @@ -261,20 +261,9 @@ Why isn't there a switch or case statement in Python? 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, like this:: - - command="a" - match command: - case "a": - function_1() - case "b": - function_2() - case "c": - self.method_1() - case _: # The "_" is a wildcard here. - print("invaild command") +Since Python 3.10 one can easily match literal values, or constants +within a namespace, with a ``match ... case`` statement. See :pep:`634` (specification) and :pep:`636` (tutorial) for details about the ``match`` statement. An older alternative is a sequence of ``if... elif... elif... else``. From 804c8a34031465ac8ba17067f0db4629017ebd2b Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Date: Wed, 7 May 2025 14:44:46 +0100 Subject: [PATCH 5/7] Update Doc/faq/design.rst Co-authored-by: sobolevn --- Doc/faq/design.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst index 99b67a065ab7a3..45cf6ce44ab931 100644 --- a/Doc/faq/design.rst +++ b/Doc/faq/design.rst @@ -264,7 +264,8 @@ 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. -See :pep:`634` (specification) and :pep:`636` (tutorial) for details about the ``match`` statement. +See :pep:`634` (specification) and :pep:`636` (tutorial) for details about +the :keyword:`match` statement. An older alternative is a sequence of ``if... elif... elif... else``. From 05a62ee1ba25cd5407e3a99613c24501a4f00bcc Mon Sep 17 00:00:00 2001 From: xzkdeng Date: Fri, 9 May 2025 18:51:38 +0800 Subject: [PATCH 6/7] Update Doc/faq/design.rst,by suggestions -1 Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> --- Doc/faq/design.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst index 45cf6ce44ab931..6542e78dd464e4 100644 --- a/Doc/faq/design.rst +++ b/Doc/faq/design.rst @@ -261,7 +261,6 @@ Why isn't there a switch or case statement in Python? 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. See :pep:`634` (specification) and :pep:`636` (tutorial) for details about From 184c150feeab6d06672802b56bc2600b763ca8ff Mon Sep 17 00:00:00 2001 From: xzkdeng Date: Fri, 9 May 2025 19:09:12 +0800 Subject: [PATCH 7/7] Update design.rst according to suggestions -2 Changed the links to link to more up-to-date documentation. --- Doc/faq/design.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst index 6542e78dd464e4..4f3133971e868d 100644 --- a/Doc/faq/design.rst +++ b/Doc/faq/design.rst @@ -263,7 +263,7 @@ 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. -See :pep:`634` (specification) and :pep:`636` (tutorial) for details about +See `the specification `_ and `the tutorial `_ for details about the :keyword:`match` statement. An older alternative is a sequence of ``if... elif... elif... else``.