Python 3.10 is Released

Here’s a rundown of all of the major new features in Python 3.10 with me including how they should be able to help your code.

Python 3.10 is Released

Python 3.10 – the latest in-development version of Python – has officially released the first beta version. With the new beta release – the feature set for Python 3.10 has been locked and set – and they recommend that all Python Developers to test their code against the latest builds in their testing environments. As of right now, production environments is not encouraged.

There aren’t many ground changing new major features in Python 3.10 – but of the few that we do have that is highly notable is structural pattern matching, This could also be  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 me including how they should be able to help your code.

Structural Pattern Matching

With several major fails 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 that is in other programming languages. It can also allows you to match against patterns of values — as an example, an object with a certain property set to a certain value. This can greatly expands the range of possibilities for developers, and makes it possible to write code that quickly cover a variety of scenarios.

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")

Error Reporting Improvements

Python’s error reporting has long been at the mercy of the whims of its parser. Also, I have complained about it throughout the history of trying to learn the language and what I was doing wrong. 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.

The new parser offers developers is far more precise and useful error messages. In Python 3.8, the following code would generate a syntax error.

python
print ("Hello"
print ("What's going on?")

  File ".\test.py", line 2
    print ("What's going on?")
    ^
SyntaxError: invalid syntax

The error that the code gave was never really that useful. It had to hunting anywhere and everywhere in your source code to find what you did wrong. Python 3.10 generates a far more useful error:

  File ".\test.py", line 1
    print ("Hello"
          ^
SyntaxError: '(' was never closed

A lot of the errors produced by the parser have been improved in this version — not only delivering more precise information about the error, but more precise information about where the error is actually occurring in the code.

Other Major Changes

  • Union types can now be expressed as X|Y, instead of Union[X,Y], for brevity (PEP 604).
  • The zip built-in, which braids together the results of multiple iterables, now has a strict keyword. When set to True, it causes zip to raise an exception if one of the iterables is exhausted before the others (PEP 618).
  • with statements now support multi-line, parenthetical syntax (BPO-12782).
  • Variables can now be declared as type aliases, to allow forward references, more robust errors involving types, and better distinctions between type declarations in scopes (PEP 613).
  • OpenSSL 1.1.1 or newer is now required to build CPython. This modernizes one of CPython’s key dependencies (PEP 644).