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 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":
        print("OK")
    case "404":
        print("Not Found")
    case "444":
        print("I'm a salt shaker")
    case _:
        print("Code not found")Code language: PHP (php)

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
):Code language: JavaScript (javascript)

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)

print(list(result))Code language: PHP (php)

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)

print(list(result))Code language: PHP (php)

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.

print("Hello"
print("World)Code language: PHP (php)

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

File "example.py", line 1
    print("Hello"
         ^
SyntaxError: '(' was never closedCode language: JavaScript (javascript)

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) #issubclassCode language: PHP (php)

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.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%