Python 3.10 is Here, Brings Some Great New Features and Improvements

Python 3.10 sports powerful pattern matching features, better error reporting, and more. Here's what's new.

Python 3.10

Python 3.10 sports powerful pattern matching features, better error reporting, and more. Here’s what’s new.

Python is a powerful multipurpose programming language. This means it’s designed to be used in a range of applications, including data science, software and web development, automation, and generally getting stuff done.

According to the TIOBE index for October 2021, which ranks programming languages based on their popularity, has Python ranked in the number 1 slot.

Python 3.10 was released yesterday, so let’s take a quick look over all the important changes.

Python 3.10 Highlights

Structural Pattern Matching

The biggest new feature in Python 3.10, probably both in terms of controversy and potential impact, is structural pattern matching. It lets you match variables against a set of different possible values like the switch-case in other languages. You can use it with the match statement and case statements of patterns with associated actions.

Here’s s the new match-case statement:

http_code = "418"

match http_code:
    case "200":
    case "404":
        print("Not Found")
    case "444":
        print("I'm a salt shaker")
    case _:
        print("Code not found")

Structural pattern matching is a comprehensive addition to the Python language. It is certainly the most interesting new feature in Python 3.10.

Parenthesized Context Managers

Now you can use enclosed parentheses around context managers when you use the with statements. This allows formatting a long collection of context managers in multiple lines.

with (
    CtxManager1() as ctx1,
    CtxManager2() as ctx2

Strict Argument for zip Function

An optional strict boolean keyword was added to the zip method to ensure all iterables have the same length. Zip creates one single iterator that aggregates elements from multiple iterables. The default behavior is simply to stop when the end of the shorter iterable is reached.

names = ['John', 'Nicole', 'Nick']
ages = [27, 31]

result = zip(names, ages)


Like in the example above, it just combined the first two elements and discarded the third name. With the new parameters strict=True this will now raise a value error if the iterables do not have the same length.

names = ['John', 'Nicole', 'Nick']
ages = [27, 31]

result = zip(names, ages, strict=True)


Python 3.10 Brings an Improved Error Messages

Another big improvement in Python 3.10 that is really helpful is improved error messages. Many of the error messages have been improved not only delivering more precise information about the error but also more precise information about where the error actually occurs.

For example in the code shown below with a missing parentheses the old error was just an invalid syntax message not even with the correct line number.


Now we can see the correct line number, the correct position and the good error description.

File "", line 1
SyntaxError: '(' was never closed

This could be especially helpful for beginners that previously oftentimes only got confused by the error message.

New Typing Features

Python 3.10 brings a new type union operator which enables the syntax X | Y. This provides a cleaner way of expressing either type X or type Y instead of using typing.Union.

Additionally this new syntax also accept as the second argument to isinstance and issubclass.

def square(number: int | float) -> int | float:
    return number ** 2

isistance(1, int | str) #issubclass

Now the typing module has a special type alias which lets you decalre type aliases more explicitly. This is useful for type checkers to distinguish between type aliases and ordinary assignments.

Updates and Deprecations in Python 3.10

Python now requires OpenSSL 1.1.1 or newer. Older versions are no longer supported. This affects hashlib, hmac, and ssl module and modernizes one of the CPython’s key dependencies.

Also the entire distutils package is deprecated and will be removed in Python 3.12. In Python 3.10 no new modules are added but a lot of modules have been improved.

So, they’re some of the key new features being introduced with Python 3.10. For more information about all changes in the new version, you can refer to the full changelog.

Leave a Reply

Your email address will not be published.

Latest from News