Python 3.10: Cool New Features for You to Try

5 minutes read

Python 3.10

High-level, general-purpose programming language Python emphasises code readability with the use of indentation. According to the TIOBE Index for October 2021, Python is the most popular programming language in the global developers’ community.

 

Python released its latest upgrade, Python 3.10, on October 04, 2021, soon after the 30th anniversary of the programming language. While there are several new features in the latest in-development version, the most significant upgrade has to be the language syntax, since async. 

 

Python 3.10, the latest release of Python, is in its beta phase. This article explores the major changes and updates to the previous version.

 

List of new features

  • Precise Error Messages
  • Population Count
  • New Type Union Operator
  • Strict Zipping
  • Deprecation and Removal of Some Backward Compatibilities
  • New syntax features 
  • PEP 634: Structural Pattern Matching- Specification 
  • Bpo-12782: Parenthesized context managers (This feature allows formatting a long collection of context managers in multiple lines).
  • PEP 626 – Add Optional Length – Checking to zip 
  • Interpreter improvements
  • PEP 626 – precise line numbers for debugging and other tools. 
  • New typing features 
  • PEP 613 – Explicit type aliases 
  • PEP 612 – Parameter specification variables 
  • Important deprecations, removals or restrictions: 
  • PEP 644 – Require OpenSSL 1.1.1 or newer
  • PEP 632 – Deprecate distutils module 
  • PEP 623 – Deprecate and prepare for the removal of the wstr member in PyUnicodeObject 
  • PEP 624 – Remove Py_UNICODE encoder APIs
  • PEP 597 – Add optional EncodingWarning

 

 Structural Pattern Matching 

Structural pattern matching has been added in the form of two statements — ‘match statement’ and ‘case statements’ of patterns with associated actions. Patterns for comparison can consist of sequences — lists and types; mapping structures — dictionaries; primitive data types or class instances. The syntax around the two statements — ‘match’ and ‘case’ expanded, can now be used to extract information from complex data types, branch the structure of data or apply other specific actions to varied data forms.

The generic syntax of pattern matching is as follows: 

Thus, pattern matching operated by: 

  • Using data with type and shape (the subject) 
  • Evaluating the subject in the match statement 
  • Comparing the subject with each pattern in case statement from top to bottom until a match is confirmed 
  • Executing action associated with a pattern of the confirmed match 
  • If the exact match is not confirmed, a wildcard when provided, can be used as the matching case.

 

Precise Error Messages

In previous versions of Python, debugging a code is pretty difficult due to poor error messages. Error messages generated by the Parser from previous versions of Python are not specific.

Consider the code below:

print(“Hello Reader”

print(“Welcome to The Chief IO”)

#The Error Message

File “<string>”, line 2

    print(“Welcome to The Chief IO”)

    ^

SyntaxError: invalid syntax

The error message generated points that the error is in line 2, whereas the actual error is in line 1. This makes debugging hard.

Python 3.10 proposes a more accurate and line-precise error message, assisting you in figuring out where the problem is from.

For the same example above, Python 3.10 will generate the following error message which is clearer;

File “<string>”, line 1

    print(“Hello Reader”)

    ^

SyntaxError: ‘(’ was never closed.

 

Population Count

A new method, bit_count, is introduced in Python 3.10. This method will return the number of ones present in the binary representation of an integer.This method is also known as Population Count (popcount) and it gives you a faster way of counting non-zero bits in an integer.

 

New Type Union Operator

Python 3.10 also comes with an improved type union checking syntax that will help you write cleaner codes.  Instead of using typing.Union to compare two data types, python 3.10 enables you to use the or (|) operator with type annotations and also functions like isinstance() and issubclass().

Here is an example of the old syntax;

def func(value: Union[int, float]) -> Union[int, float]:

    return value

With the new | operator, we can write the same code like this;

def func(value: int | float) -> int | float:

    return value

Here is an example of the usage with isinstance() function;

isinstance(“hello”, int | str)

# True

 

Strict Zipping

A new optional Keyword argument, strict, is added to the zip function in Python 3.10. If you set the strict keyword to true, the iterables that you are zipping must be of equal lengths, or else, an error value will be raised.

 

Deprecation and Removal of Some Backward Compatibilities

Some earliest support for abstract base classes and old import semantics will be dropped from Python 3.10 onwards.

Some of the deprecated syntax includes find_loader()/find_module, load_module and _load_attribute.

Support for Distutils is also discontinued and replaced with setuptools and packaging.

 

Other major changes in Python 3.10

  • Union types can now be expressed as X|Y, instead of Union[X,Y], for brevity (PEP 604).
  • The zip built-in, which braids together the results of multiple iterables, now has a strict keyword. When set to True, it causes zip to raise an exception if one of the iterable is exhausted before the others (PEP 618).
  • with statements now support multi-line, parenthetical syntax (BPO-12782).
  • Variables can now be declared as type aliases, to allow forward references, more robust errors involving types, and better distinctions between type declarations in scopes (PEP 613).

OpenSSL 1.1.1 or newer is now required to build CPython. This modernizes one of CPython’s key dependencies (PEP 644).

 

Related Posts...

Web DesignWebsite developmentWhat is New!What's Hot