Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion Doc/faq/design.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down