Python 3.10, do you understand the switch syntax?

Python is the programming language of choice for people working in data science and artificial intelligence. According to a recent survey, 27% of programmer development jobs require knowledge of the Python language, up from 18.5% at the beginning of this year.

What makes Python popular is its ability to be very intuitive: the language has a large number of libraries, is productive enough, and is relatively easy to learn. In October 2020, version 3.9 of Python was officially released, from dictionary updates/merging to adding new string methods, to the introduction of the zoneinfo library, Python 3.9 added many new features.

The second alpha version of Python 3.10 was also released in early November 2020. Compared with the 3.9 version released not long ago, the new version has new improvements in type annotation extensions, zip, bit count, and dictionary mapping. Python 3.10 beta version is released, and the biggest highlight of the new beta version may be the introduction of the switch-case statement.

New improvements in Python 3.10 beta

The Switch statement exists in many programming languages, but the Python programming language does not support the Switch statement. Back in 2016, PEP 3103 was proposed, suggesting that Python support switch-case statements. However, a survey found very little support for the feature, and Python developers abandoned it.

The time is pushed to 2020. Guido van Rossum, the founder of Python, submitted the first document showing the switch statement, named Structural Pattern Matching, see PEP 634.

Now, with the release of Python 3.10 beta, switch-case statements are finally included.

Parenthesized context managers: Parentheses are now supported for continuations across multiple lines in a context manager. You can also use a comma at the end of the included group.

with (
CtxManager1 as example1,
CtxManager2 as example2,
CtxManager3 as example3,
):...

Error message - NameErrors: When printing a NameError raised by the interpreter, PyErr_Display will suggest similar variable names in the function that raised the exception:

PEP 634 Structural Pattern Matching: Pattern matching allows users to follow a match with several case statements. When a match-case is executed in the program, if there is a matching statement, the program will enter the corresponding case statement to execute the operation.

match-case syntax and operations: The general syntax for pattern matching is:

match subject:
case <pattern_1>:
<action_1>
case <pattern_2>:
<action_2>
case <pattern_3>:
<action_3>
case _:
<action_wildcard>

The match statement takes an expression and compares its value against a consecutive pattern given as one or more case blocks. An example of match-case is as follows:

http_code = "418"
match http_code:case "200":
print("OK")
do_something_goodcase "404":
print("Not Found")
do_something_badcase "418":
print("I'm a teapot")
make_coffeecase _:print("Code not found")

The following figure is a schematic diagram of the execution of the match-case statement. The program checks for multiple case conditions and performs different actions depending on the value found in the variable http_code.

Likewise, you can build the same logic using a set of if-elif-else statements:

http_code = "418"
if http_code == "418":
print("OK")
do_something_goodelif http_code == "404":
print("Not Found")
do_something_badelif http_code == "418"
print("I'm a teapot")
make_coffeeelse:print("Code not found")

However, by using the match-case statement, the repeated execution of http_code == is removed, and http_code == looks much cleaner when using match-case when testing many different conditions.

We can understand pattern matching with a simple example: matching objects (data objects) against text (patterns) using switch statements in C, Java, or JavaScript (and many other languages). A switch statement is often used to compare an object/expression with a case statement that contains literals.

While an imperative series of instructions using nested if statements can be used to accomplish tasks similar to structural pattern matching, it is not as clear as a declarative approach. In contrast, a declarative approach declares the conditions that need to be met for a match and is more readable through its explicit pattern. While structural pattern matching can be used in its simplest form, comparing a variable to the text in a case statement, its real value to Python lies in its handling of object types and sizes.

match-case can be said to be the biggest highlight of this Python 3.10 beta version. Some people like this expression and some people hate it. Before Python does not support switch-case, everyone may use dictionaries for related operations. Some people say that switch has no advantages other than being easy to read; some people say that Python's closure mechanism, the value of dict can be a function with a closure, which makes the expressive ability to a higher level than switch-case; but after all, Python 3.10 The beta version incorporates switch-case, giving developers another option.

Here I would like to recommend the Python learning Q group I built by myself: 1020465983. Everyone in the group is learning Python. If you want to learn or are learning Python, you are welcome to join. Everyone is a software development party and shares dry goods from time to time ( Only related to Python software development),
including a copy of the latest Python advanced materials and zero-based teaching in 2021 that I have compiled by myself. Welcome to the advanced middle and small partners who are interested in Python!

Guess you like

Origin blog.csdn.net/weixin_56659172/article/details/124165838