What’s New in Python 3.10?

September 30, 2021

Python 3.10 is the latest version of Python and has been released in its first beta version. With the release of the beta version, the set of features for Python 3.10 has been finalized and the audacious Python developers are encouraged to examine their code with their latest builds. There are some interesting additions in Python which we are going to cover - structural pattern matching, more precise error reporting, parameter specification variables and other major changes.

Here’s a synopsis of all the major new features in Python 3.10 and how they can help your code.

Structural Pattern Matching: There were many failed attempts to add a case-like or switch syntax to Python and structural pattern matching is the outcome of such attempts. It enables you to match variables with one set of possible values with case/switch in other languages. It also enables you to match with the patterns of values like an object to set a certain value with a certain property. This enhances the range of possibilities to a great extent and makes it possible to write a code that quickly includes various scenarios. One of the examples is shown below –

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

Precise Error Reporting: The error reporting with Python was dependent on the impulse of the parser for so long. Python 3.9 has unfolded an entirely new parser that is more robust, faster and easier for the team of Python to maintain and be less corrupted with internal hacks. The biggest advantage of the new parser is that it offers the developers far more useful error messages which are also more precise. In the 3.8 version of Python, the following code would generate a syntax error.

print ("Hello"

print ("What's going on?")

File ".\test.py", line 2

print ("What's going on?")

^

SyntaxError: invalid syntax

The error message is not so helpful as the real problem lies in an earlier line. The latest version of Python generates a much more useful error.

File ".\test.py", line 1

print ("Hello"

^

SyntaxError: '(' was never closed

In this vein, most of the errors produced by the parser have been improved, they not only deliver accurate information about the error but more precisely talk about where the error occurred.

Parameter Specification Variables: The typing module of Python is used to illustrate the code with type information and lets you outline the type of callable like a function. That type of information cannot be spread across all the callable which makes it difficult to illustrate things like function decorators. There are two new additions to typing typing.Concatenate and typing.ParamSpec makes it possible to explain callable with more abridged type definition information. Here is an example of this new feature –

from typing import Awaitable, Callable, TypeVar

R = TypeVar("R")

def add_logging(f: Callable[..., R]) -> Callable[..., Awaitable[R]]:

async def inner(*args: object, **kwargs: object) -> R:

await log_to_database()

return f(*args, **kwargs)

return inner

@add_logging

def takes_int_str(x: int, y: str) -> int:

return x + 7

await takes_int_str(1, "A")

await takes_int_str("B", 2) # fails at runtime

As it is not possible to specify the liner with proper details regarding what kind of types are being passed to the functions that are handled by the decorator, the liner is not able to catch the invalid types in the second occurrence of takes_int_str. Below is shown how the code would look with the latest parameter specification variable syntax.

from typing import Awaitable, Callable, ParamSpec, TypeVar

P = ParamSpec("P")

R = TypeVar("R")

def add_logging(f: Callable[P, R]) -> Callable[P, Awaitable[R]]:

async def inner(*args: P.args, **kwargs: P.kwargs) -> R:

await log_to_database()

return f(*args, **kwargs)

return inner

@add_logging

def takes_int_str(x: int, y: str) -> int:

return x + 7

await takes_int_str(1, "A") # Accepted

await takes_int_str("B", 2) # Correctly rejected by the type checker

ParamSpec enables us to indicate where to capture positional and keyword arguments. Concatenate can be used to pinpoint how arguments are removed or added.

Other Major Changes:

  • Instead of Union [X, Y], Union types can now be expressed as X|Y for brevity.
  • The built-in zip has now a strict keyword that puts together the results of various iterables. When True is set, the zip raises an exception if any one of the iterable is used up before the others.
  • Parenthetical syntax and multi-line now support
  • Variables are now stated as type aliases, they enable forward references, the better difference between type declaration in scopes and more robust errors which involve types.
  • To build CPython, SSL 1.1.1 or a newer version is now required which revamps one of the key dependencies of CPython.

Final Words

These were some of the important new features introduced with Python 3.10. The full release of this version is expected in October 2021 between which the Python developers will be working on enhancing what has been added. It is still in the alpha version and still far away from being fully tested and ready to use.

About Digital Crafters

Being a leading Python web and software development company, we deliver best-in-class web applications using outstanding Python development frameworks, programming language, Databases like Django and other advanced tools. The dedicated full-stack Python developers with us work exceptionally to give amazing results for the client's Python application development project. We perceive the business of the client as our own and transform the out of the box ideas into profitable web applications and websites.

Write to us at info@digitalcrafters.tech for more details about our services.

Copyright © 2024 All rights reserved Digital Crafters

Technologies

Design

Quality Assurance

Enterprise & SaaS Solutions

Mobile App Development

E-COMMERCE

Data Engineering & Analytics

Cloud Engineering

Devops 

Web Technologies

IT staff Augmentation