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.
Today, we list the major features of Python 3.10.
List of new features
- New syntax features
- PEP 634: Structural Pattern Matching- Specification
- PEP 635: Structural Pattern Matching- Motivation and Rationale
- PEP 636: Structural Pattern Matching- Tutorial
- Bpo-12782: Parenthesized context managers (This feature allows formatting a long collection of context managers in multiple lines).
- New feature in the standard library
- PEP 626 – Add Optional Length – Checking to zip
- Interpreter improvements
- PEP 626 – precise line numbers for debugging and other tools.
- New typing features
- PEP 604 – Allowing writing union types as X | Y
- 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 (PEP 634) in Python allows one to match variables against patterns of values — an object with a certain property set to a certain value. This update expands the range of possibilities and is going to benefit those who deal intensively with data structures. That is, it makes it possible to write code quickly encompassing a variety of scenarios.
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.
The development team at Python has improved its debugging capabilities and clarity of error messages.
Earlier, while programming in Python, developers would get vague messages like:
‘SyntaxError: unexpected EOF while parsing’
However, in version 3.10, the interpreter will now provide more precise and specific information while parsing the code. For instance, when a programmer misses using ‘:’ before blocks, the interpreter will display the following message:
SyntaxError: expected ‘:’
Additionally, the latest version simplifies the static type system. That is, in order to simplify coding, without losing type information, with the help of ‘float | int’ substitute. Type hinting will allow programmers to ‘list’ instead of Python 3.9’s ‘typing.List’ use.
Parameter Specification Variables
Developers have added two new options to improve the information provided to static type checkers for ‘Callable’ to the ‘typing’ module. The first is the parameter specification variable which is used to forward the parameter types of one callable to another, a pattern usually found in higher-order functions and decorators.
The second one, the ‘Concatenate’ operator, is used in conjunction with parameter specification variables to annotate a higher-order callable which adds or removes parameters of another callable.