Ah, there it is: Python 3.10 launches switch equivalent, adds more help in case things go wrong

Ah, there it is: Python 3.10 launches switch equivalent, adds more help in case things go wrong

Version 3.10 of popular programming language Python has been released just as planned, and while the update doesn’t bring a ton of changes, new syntax and typing features are definitely interesting enough to make it worth your while.

The big feature of the release definitely has to be structural pattern matching. The addition is meant to provide Python developers with an easier way to extract information from complex data types and apply actions based on the forms of data (hence the structural part of the name). Structural pattern matching matches dictionary structures with binding and can be compared to a switch statement in C and Java, so that developers don’t have to rely on if/else/elif blocks for handling expression values anymore. 

The feature is implemented via a match statement that “takes an expression and compares its value to successive patterns given as one or more case blocks”. A basic example from (one of) the associated PEP(s), looks something like:

#point is an (x, y) tuple

match point:
     case (0, 0):
          print("Origin")
     case (0, y):
          print(f"Y={y}")
     case (x, 0):
          print(f"X={x}")
     case (x, y):
          print(f"X={x}, Y={y}")
     case _:
          raise ValueError("Not a point")

The underscore (_) in the last case of the example is a wildcard, which already shows one of the peculiarities of the enhancement that led to at least one of many discussions during the design stage of the enhancement. It has to be said that the use isn’t entirely intuitive, as the fact that you can use the walrus operator in the match statement, but not in the case statement shows. However, the Python team did its best to explain the new addition and provided an in-depth tutorial to help developers get to grips with the new syntax.

Other than that, Python 3.10 includes a new type union operator that allows developers to write X|Y instead of Union [X,Y] to say that either type X or Y would be an option in type hinting scenarios and a TypeAlias value to declare type aliases making them easier to distinguish from regular assignments. There’s also the addition of parameter specific variables in the typing module, however these might be more of interest to tool builders.

Thanks to some work on the interpreter, Python code should become a bit easier to debug starting with this release, since error messages will finally point to where the error actually is instead of leading you to the next line. Another improvement in that area is the introduction of more helpful error messages. 

For instance, the Python team added more context and additional hints to syntax errors, so that developers will be quicker to find unclosed brackets, missing colons before blocks and in dictionary literals, and missing commas in collection literals. PyErr_Display() also learned to use object or function information to offer suggestions for similar attribute or variable names when attribute or name errors are raised, which should be helpful if you tend to misspell things or have to work with unfamiliar code a lot.

A full list of changes, which also include some optimisation in various constructors to speed up code, can be found in the Python documentation. It also comes with a medium-length sized list of deprecations, the most noteworthy being a number of threading methods and syntax that allows you to follow numeric literals by keywords and, else, for, if, in, is, and or which will soon result in an error. Users also should be aware that Python 3.10 requires OpenSSL 1.1.1 or newer to work, though that is something hopefully most have switched to already.

With Python 3.10 being officially available, it will be interesting to see if the new features can inspire enough articles and online discussions to bump the language up the TIOBE Programming Community index. The September edition saw Python only needing 0.16% to surpass C in popularity — and given the way the ranking is constructed, it should be quick to capture such a development.

Update: The October version of the TIOBE Index has just been released and indeed sees Python on top. This however seems to be more due to a slight dwindling of interest in C, which dropped from its 11.83% rating last month to 11.16%, thus coming in second after Python with 11.27% (11.67% last month).