3

Python News: What's New From October 2021?

 2 years ago
source link: https://realpython.com/python-news-october-2021/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The Python 3.10 Release

New versions of Python are now released annually. We can look forward to the core developers sharing a lovely goody bag with the rest of us every October. With Python 3.10, which came out of beta on October 4th, everyone had something exciting to anticipate.

Each release of Python has a release manager who’s responsible for coordinating all changes and for building and preparing the files for distribution. The release manager for Python 3.10 and 3.11 is Pablo Galindo Salgado. In a first for Python, he built and released Python live on YouTube.

Python 3.10 Highlights

The new release includes lots of improvements to the language. Among our favorites are improved error messages, simplified syntax for type unions, and structural pattern matching.

Improved error messages will make your life easier, whether you’re a new Python developer or an experienced one. In particular, the feedback that you get when your code isn’t valid Python is more pointed and actionable in Python 3.10 than in previous versions. As an example, consider the following code, where there’s no closing bracket at the end of the first line:

news = ["errors", "types", "patterns"
print(", ".join(news))

In Python 3.9 and earlier, you’ll see the following if you try to run this code:

  File "errors.py", line 2
    print(", ".join(news))
        ^
SyntaxError: invalid syntax

The invalid syntax explanation isn’t very insightful. To make matters worse, the reported line number is wrong. The actual error happens on line 1—not on line 2, as the error message says. The new parser, which was introduced in Python 3.9, allows for much better feedback:

  File "errors.py", line 1
    news = ["errors", "types", "patterns"
           ^
SyntaxError: '[' was never closed

The line number is correct, and the accompanying explanation is to the point. This will allow you to jump right in, fix the error, and carry on coding!

Simplified syntax for type unions allows you to use type hints, often without any extra imports. You can use type hints to annotate your code, get increased support from your editor, and catch bugs earlier.

The typing module is a centerpiece for adding static types to Python. However, over the last few releases, more and more tools have moved from typing to built-in functionality. In Python 3.10, you’re allowed to use the pipe operator (|) to specify type unions instead of importing Union from typing. The following code snippet shows an example of the new syntax:

def mean(numbers: list[float | int]) -> float | None:
    return sum(numbers) / len(numbers) if numbers else None

The annotation of number specifies that it’s supposed to be a list of float and int objects. Previously, you might have written this as List[Union[float, int]]. Similarly, the annotation of the return value, float | None, is a special case of a type union that you can also write as Optional[float]. The new syntax means that you can annotate a lot of your code without even importing typing.

Structural pattern matching is a powerful way of working with data structures that you may know from functional languages like Elixir, Scala, and Haskell. We previewed this feature in our newsletters in March and August.

Structural pattern matching is at its best when you need to manipulate lists, dictionaries, data classes, or other structures. The following example implements a recursive function that sums a list of numbers. It gives you a quick look at the new syntax:

def sum(numbers, accumulator=0):
    match numbers:
        case []:
            return accumulator
        case [head, *tail]:
            return sum(tail, accumulator + head)

This code uses accumulator to keep track of the running total. You match numbers to two different cases.

In the first case, numbers is an empty list. Since you don’t need to add more to your sum, you can return accumulator. The second case accounts for what to do when there’s at least one element in the list: you name the first element head, and you name the rest of the list tail. You add head to your running total and then recursively call sum() for the remaining elements.

You can implement the same algorithm using if statements. However, the new syntax opens up a more functional way of thinking about Python code, which can be an interesting avenue to explore going forward.

Dig into the details of these improvements, along with all the other new features in Python 3.10, in our dedicated tutorial.

A Live Python 3.10 Release Party on YouTube

Usually, the actual release of a new Python version happens behind closed doors. Although it’s announced well in advance, the link to download the new version tends to appear rather suddenly.

This year was different! Release manager Pablo Galindo Salgado and Leon Sandøy from Python Discord invited everyone to a release party live on YouTube. Despite the Internet having a bad day, the live stream worked out great and we could all watch as Pablo ran his magical scripts that make Python available for the whole world.

In addition to Pablo and Leon, several other core contributors joined the party:

The stream is still available. Check it out if you’re interested in getting a unique look under the hood and seeing what it takes to release a new version of Python.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK