Python 3.10 is here, and switch syntax is finally here

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. Last October, Python version 3.9 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 last year. Compared with the 3.9 version released not long ago, the new version has new improvements to type annotation extensions, zip, bit count, and dictionary mapping. Just yesterday, Python 3.10 beta was released, and perhaps the biggest highlight of the new beta is 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_good()
    case "404":
        print("Not Found")
        do_something_bad()
    case "418":
        print("I'm a teapot")
        make_coffee()
    case _:
        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_good()
elif http_code == "404":
    print("Not Found")
    do_something_bad()
elif http_code == "418"
    print("I'm a teapot")
    make_coffee()
else:
    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.

At the end, I will also give you a python spree [Jiajun Yang: 419693945] to help you learn better!

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324342094&siteId=291194637