Python 3.10 runs switch equivalent, adds more help if something goes wrong • DEVCLASS
Version 3.10 of the popular Python programming language has been released as expected, and while the update doesn’t bring a ton of changes, the new syntax and typing features are cool enough to make it worth it.
The great feature of the version must definitely be the correspondence of the structural patterns. The addition is intended to provide Python developers with an easier way to extract information from complex data types and apply actions based on the data shapes (hence the structural part of the name). Structural model matching matches dictionary structures with binding and can be likened to a switch statement in C and Java, so developers don’t have to rely on
if/else/elif blocks to handle more expression values.
The functionality is implemented via a meet statement which “takes an expression and compares its value to successive models given in the form of one or more blocks of cases”. A basic example of (one of) associated PEP (s) looks like this:
#point is an (x, y) tuple
case (0, 0):
case (0, y):
case (x, 0):
case (x, y):
raise ValueError("Not a point")
The underscore (
_) in the latter case of the example is a wildcard, which already shows one of the peculiarities of the improvement that led to at least one of the many discussions during the design phase of the improvement. It must be said that the use is not entirely intuitive, as shown by the fact that you can use the morse operator in the match statement, but not in the case statement. However, the Python team did their best to explain the new addition and provided an in-depth tutorial to help developers familiarize themselves with the new syntax.
Apart from that, Python 3.10 includes a new type of union operator that allows developers to write
X|Y in the place of
Union [X,Y] say that type X or Y would be an option in type indication scenarios and a
TypeAlias value to declare type aliases, which makes them easier to distinguish from normal assignments. There is also the addition of parameter specific variables in the input module, but these could be more interesting for tool builders.
With a bit of interpreter work, Python code should become a bit easier to debug from this version on, as the error messages will finally point out where the error actually is instead of leading you to the next. next line. Another improvement in this area is the introduction of more useful error messages.
For example, the Python team added more context and extra guidance on syntax errors, so developers can find unclosed brackets, missing colons before blocks and in dictionary literals faster, and the missing commas in the literals of the collection.
PyErr_Display() Also learned how to use object or function information to come up with suggestions for similar attributes or variable names when attribute or name errors are found, which should be helpful if you tend to misspell things or if you have to work a lot with unknown code.
A full list of changes, which also include optimization in various constructors to speed up the code, can be found in the Python documentation. It also comes with a list of mid-size deprecations, the most notable being a number of threading and syntax methods that allow you to track numeric literals by keywords and, if not, for, if, in, is and or that will soon result in an error. Users should also be aware that Python 3.10 requires OpenSSL 1.1.1 or newer to work, although hopefully most have already passed.
With Python 3.10 officially available, it will be interesting to see if the new features can inspire enough articles and online discussions to move the language up the TIOBE programming community index. The September edition saw Python only need 0.16% to overtake C in popularity – and given the way the ranking is constructed, it should be quick to capture such development.