The general-purpose, high-level Python programming language emphasizes code readability through the use of indentation. According to the TIOBE Index for October 2021, Python is the most popular programming language in the global developer community.
Python released its latest update, Python 3.10, on October 4, 2021, shortly after the programming language’s 30th anniversary. While there are several new features in the latest release in development, the biggest upgrade has to be the language syntax, because async.
Today we are listing the main features of Python 3.10.
List of new features
- PEP 634: Matching structural patterns – Specification
- PEP 635: Correspondence of structural models – Rationale and justification
- PEP 636: Structural Pattern Matching – Tutorial
- Bpo-12782: Context managers in parentheses (This feature allows to format a long collection of context managers on several lines).
- New functionality in the standard library
- PEP 626 – Add optional length – Check zipper
- Interpreter improvements
- PEP 626 – precise line numbers for debugging and other tools.
- PEP 604 – Allow writing union types as X | Yes
- PEP 613 – Explicit type alias
- PEP 612 – Parameter specification variables
- Significant withdrawals, deletions or restrictions:
- PEP 644 – Require OpenSSL 1.1.1 or newer
- PEP 632 – Deprecate the distutils module
- PEP 623 – Abandon and prepare deletion of wstr member in PyUnicodeObject
- PEP 624 – Remove Py_UNICODE encoder APIs
- PEP 597 – Add optional coding warning
Structural pattern matching
Structural Model Matching (PEP 634) in Python allows variables to be matched to models of values ââ- an object with a certain property set to a certain value. This update expands the range of possibilities and will benefit those who deal intensively with data structures. That is, it allows you to quickly write code encompassing a variety of scenarios.
Structural model matching has been added as two statements – âmatch statementâ and âcase statementsâ of models with associated actions. Comparison models can consist of sequences – lists and types; cartographic structures – dictionaries; primitive data types or class instances. The syntax around the two statements – extended ‘match’ and ‘cases’ can now be used to extract information from complex data types, branch into the data structure, or apply other specific actions to forms various data.
The generic syntax for pattern matching is:
Thus, pattern matching is operated by:
- Using data with type and form (the subject)
- Subject evaluation in the correspondence statement
- Compare the subject with each model in the case statement from top to bottom until a match is confirmed
- Execution of the action associated with a pattern of the confirmed match
- If the exact match is not confirmed, a wildcard, when supplied, can be used as a match.
The Python development team has improved its debugging capabilities and the clarity of error messages.
Previously, when programming in Python, developers received vague messages such as:
“Syntax error: Unexpected EOF during parsing”
However, in version 3.10 the interpreter will now provide more precise and specific information when parsing code. For example, when a programmer omits to use ‘:’ before blocks, the interpreter will display the following message:
Syntax error: ‘:’ expected
In addition, the latest version simplifies the static type system. That is, in order to simplify the encoding, without losing type information, using ‘float | int ‘substitute. The type indication will allow programmers to “list” instead of using Python 3.9’s “typing.List”.
Parameter Specification Variables
The developers added two new options to improve the information provided to static type checkers for ‘Callable’ at the ‘typing’ module. The first is the parameter specification variable which is used to pass parameter types from one callable to another, a pattern typically found in higher-order functions and decorators.
The second, the ‘Concatenate’ operator, is used in conjunction with parameter specification variables to annotate a higher order callable that adds or removes parameters from another callable.
For a detailed overview of the new features in Python 3.10 over 3.9, see here. To view the free Python courses for data scientists in 2021, click here.
Join our Discord server. Be part of an engaging online community. Join here.
Subscribe to our newsletter
Receive the latest updates and relevant offers by sharing your email.