Juha-Matti Santala
Community Builder. Dreamer. Adventurer.

Python 3.10 is out and I'm excited

If you're here because it's the first Wednesday of the month and you're looking forward to a Learning Rust blog post, I'm gonna have a break month from that this time because I haven't written any Rust in the past month. Meanwhile, enjoy while I get excited about Python's newest release.

This Monday, October 4th Python 3.10 was finally released. It's the one programming language version release that I've been most excited ever and one of the first times I've used the new version before its launch. Here are the main two reasons for my excitement:

Pattern Matching

The main reason I got excited about this release is the addition of pattern matching feature. I wrote a blog post about it already in February when it was announced and I recommend watching Daniel Moisset's talk about the pattern matching from PyAmsterdam meetup from last March and an early tutorial by Alexander Hultnér: Get started with Pattern Matching in Python, today!

To illustrate what pattern matching provides, let's take a look at this example from the PEP 636:

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

Pattern matching combines control flow (if/else) with destructuring of values. In the above example, the control flow is based on the values being 0 for both, 0 for first item, 0 for second item or anything else. The destructuring happens when we assign the variable information (x and y) to a programming language variable so we can use it inside the case block.

In 3.10 Release Stream, Brandt Bucher showed a few examples of how the new functionality makes the code cleaner and easier to read, here's one:

# Python 3.10
match meal:
  case entree, side:
    ...

# Python 3.9
if (
  isinstance(meal, Sequence)
  and len(meal) == 2
):
  entree, side = meal
  ...

I've been using pattern matching a lot with Rust and I'm so happy it's finally available in Python. I believe it will make some parts of the code nicer to write and read in the long run. That means more focus on what the code achieves and less on how it's implemented.

Error message improvements

Something I've been following on Twitter as part of 3.10's development has been the work done on improving error messages. Error messages in many technical products, whether they are programming languages, tools or services, are often quite cryptic and unless you already know what they mean, they be hard to understand and I've seen a lot of beginners just ignore them.

Here are some examples from the What's New in Python 3.10 page:

Unclosed curly braces

File "example.py", line 1
    expected = {9: 1, 18: 2, 19: 2, 27: 3, 28: 3, 29: 3, 36: 4, 37: 4,
               ^
SyntaxError: '{' was never closed

Missing colon at the start of a block

>>> if rocket.position > event_horizon
  File "<stdin>", line 1
    if rocket.position > event_horizon
                                      ^
SyntaxError: expected ':'

Missing commas

>>> items = {
... x: 1,
... y: 2
... z: 3,
  File "<stdin>", line 3
    y: 2
       ^
SyntaxError: invalid syntax. Perhaps you forgot a comma?

IndentationErrors

>>> def foo():
...    if lel:
...    x = 2
  File "<stdin>", line 3
    x = 2
    ^
IndentationError: expected an indented block after 'if' statement in line 2

And there are more. I'm sure these will have a big impact on especially new developers' journey with Python. Learning from helpful error messages is a great way to become a better developer and I'm thankful for the work Pablo Galindo and Batuhan Taskaya has put on these.

The other stuff

Python 3.10 also brings improvements on typing with X | Y union type declarations, explicit type aliases and parameter spefication variables.

To find out about all the good stuff coming to Python with the newest release, read the docs.

3.10 Release Stream

If you're interested in seeing what a release of a new version looks like, watch the Python 3.10 Release Party recording from Monday. It was really cool to see the process and what happens behind the scenes. They did a great job combining the actual "here's how we build and publish a new version" process with presentations on the new features.

I found it not only enjoyable but a really great way to understand what new features are being added, how they function and what were the design decisions behind them.