What’s new in Python 3.10


Python 3.10, the latest in-development version of Python, has been released. Intrepid Python developers are encouraged to test their code against it, with proper precautions (e.g., using a virtual environment).

There aren’t many truly new major features in Python 3.10, but of the few that we do have, one of them — structural pattern matching — may be the single most significant addition to the language syntax since async.

Here’s a rundown of all of the major new features in Python 3.10, with discussion of how they can help your code.

Structural pattern matching

An outgrowth of previous failed attempts to add a switch/case-like syntax to Python, structural pattern matching lets you match variables against one of a set of possible values (as with switch/case in other languages). But it also allows you to match against patterns of values — e.g., an object with a certain property set to a certain value. This greatly expands the range of possibilities, and makes it possible to write code that quickly encompasses a variety of scenarios. For example:

command = input()
match command.split():
case ["quit"]:
quit()
case ["load", filename]:
load_from(filename)
case ["save", filename]:
save_to(filename)
case _:
print (f"Command '{command}' not understood")

For more details on how to use pattern matching, see our how-to on this new syntax.

More precise error reporting

Python’s error reporting has long been at the mercy of the whims of its parser. Python 3.9 rolled out an entirely new parser — faster, more robust, easier for the Python team to maintain, and less riddled with internal hacks.

Copyright © 2021 IDG Communications, Inc.



Source link