Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use switch syntax in Python 3.10

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to use Python 3.10 switch syntax, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

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

The reason why Python is popular is that it is very intuitive: the language has a large library, is productive enough, and is relatively easy to learn. Last October, Python version 3.9 was officially released, with many new features added from dictionary update / merge to the addition of new string methods to the introduction of the zoneinfo library.

The second alpha version of Python3.10 was also released in early November last year, with new improvements to type annotation extension, zip, bit counting, and dictionary mapping compared to the recently released 3.9 version. Just yesterday, the beta version of Python 3.10 was released, and the biggest highlight of the new beta version may be the introduction of switch-case statements.

New improvements to Python 3.10 beta version

Switch statements exist in many programming languages, but the Python programming language does not support Switch statements. As early as 2016, PEP 3103 was proposed that Python support switch-case statements. However, the survey found that few people supported the feature, and Python developers abandoned it.

In 2020, Guido van Rossum, the founder of Python, submitted the first document to display the switch statement, named Structural Pattern Matching, see PEP 634.

Now, with the release of Python 3.10 beta, the switch-case statement is finally included.

Context manager with parentheses: continuation using parentheses across multiple lines in the context manager is now supported. 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 thrown by interpreter, PyErr_Display () will provide advice on similar variable names in the function that throws the exception:

PEP 634 structural pattern matching: pattern matching allows the user to follow several case statements after match. When the program executes match-case and there is a matching statement, the program enters the corresponding case statement to perform the operation.

Match-case syntax and operations: the general syntax for pattern matching is:

Match subject: case: case _:

The match statement accepts an expression and compares its value with a contiguous 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 match-case statement execution. The program examines multiple case conditions and performs different actions based on the values found in the variable http_code.

Similarly, you can use a set of if-elif-else statements to build the same logic:

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 it seems clearer to use match-case,http_code = = when testing many different conditions.

We can learn about pattern matching with a simple example: matching objects (data objects) to text (patterns) using switch statements in C, Java, or JavaScript (and many other languages). Switch statements are typically used to compare objects / expressions with case statements that contain literals.

Although imperative instruction series using nested if statements can be used to accomplish tasks similar to structural pattern matching, it is not as clear as the declarative approach. Instead, the declarative approach declares the conditions that the match needs to meet and is more readable through its explicit pattern. Although structural pattern matching can be used in the simplest form to compare variables with text in case statements, 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. For this kind of expression, some people like it, others hate it. Before Python did not support switch-case, everyone probably used dictionaries for related operations. Some people say that switch has no advantages except that it is easy to read; others say that the closure mechanism of Python, the value of dict can be a function with closure, so that the expressive ability of switch-case is higher than that of switch-case. However, the 3.10 beta version of Python is incorporated into switch-case, providing another choice for developers.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report