Release of the Python programming language version 3.10

After a year of development, a significant release of the Python programming language version 3.10 has been presented. This new branch will be supported for a year and a half, after which patches for vulnerability fixes will be developed for an additional three and a half years.

At the same time, alpha testing of the Python 3.11 branch has begun (according to the new development schedule, work on a new branch starts five months before the release of the previous branch and reaches alpha testing by the time of the next release). The Python 3.11 branch will be in alpha release for seven months, during which new features will be added and bugs will be fixed. After that, for three months, beta testing will take place, during which the addition of new features will be prohibited and all attention will be focused on bug fixes. The last two months before the release will see the branch enter the release candidate stage, where final stabilization will occur.

Among the innovations introduced in Python 3.10 are:

  • The addition of the 'match' and 'case' operators for pattern matching, which improve code readability, simplify matching arbitrary Python objects, and enhance code reliability through the possibility of extended static type checking. The implementation is reminiscent of the 'match' operator provided in languages like Scala, Rust, and F#, which compares the result of evaluating a specified expression against a list of patterns defined in case-based blocks.

    def http_error(status): match status: case 400: return "Bad request" case 401|403|404: return "Not allowed" case 418: return "I’m a teapot" case _: return "Something else"

    Unpacking of objects, tuples, lists, and arbitrary sequences for variable binding based on available values is possible. Nested templates can be defined, additional conditions like "if" can be used in templates, masks ("[x, y, *rest]") can be applied, key/value mapping (for example, {"bandwidth": b, "latency": l} to extract values for "bandwidth" and "latency" from a dictionary), subtemplate extraction (the operator ":=") can be used, and named constants can be utilized in the template. In classes, behavior customization during matching can be achieved with the method "__match__()".

    from dataclasses import dataclass @dataclass class Point: x: int y: int def whereis(point): match point: case Point(0, 0): print("Origin") case Point(0, y): print(f"Y={y}") case Point(x, 0): print(f"X={x}") case Point(): print("Somewhere else") case _: print("Not a point") match point: case Point(x, y) if x == y: print(f"Y=X at {x}") case Point(x, y): print(f"Not on the diagonal") RED, GREEN, BLUE = 0, 1, 2 match color: case RED: print("I see red!") case GREEN: print("Grass is green") case BLUE: print("I’m feeling the blues :(")

  • Support for parentheses in the with statement for splitting the definition of a collection of context managers across multiple lines has been introduced. It is also permitted to leave a comma after the final context manager in the group: with ( CtxManager1() as example1, CtxManager2() as example2, CtxManager3() as example3, ): …
  • Improved error location reporting in the code for issues related to unclosed curly braces and quotes in string literals. For example, when there is an unclosed curly brace, instead of reporting a syntax error in the following statement, the pointer now highlights the opening curly brace and indicates the absence of a closing block. File "example.py", line 1 expected = {9: 1, 18: 2, 19: 2, 27: 3, 28: 3, 29: 3, 36: 4, 37: 4, ^ SyntaxError: '{' was never closed

    Additional specialized syntax error messages have been added: missing ':' before a block and in dictionaries, not enclosing tuples in parentheses, missing commas in lists, specifying a 'try' block without 'except' and 'finally', using '=' instead of '==' in comparisons, specifying *-expressions in f-strings. Furthermore, the entire problematic expression is highlighted, not just its beginning, and errors related to incorrect indentation are now more clearly contextualized. >>> def foo(): … if lel: … x = 2 File "", line 3 x = 2 ^ IndentationError: expected an indented block after ‘if’ statement in line 2

    For errors caused by typos in attribute names and variable names within functions, a suggestion with the correct name has been provided. >>> collections.namedtoplo Traceback (most recent call last): File "", line 1, in AttributeError: module ‘collections’ has no attribute ‘namedtoplo’. Did you mean: namedtuple?

  • For debugging tools and profilers, the trace events now specify the exact line numbers of executed code.
  • A new setting sys.flags.warn_default_encoding has been added to issue warnings about potential errors related to handling TextIOWrapper and open() files in UTF-8 encoding without explicitly specifying the option 'encoding="utf-8"' (ASCII encoding is used by default). Additionally, the new release allows specifying 'encoding="locale"' to set the encoding based on the current locale.
  • The typing module, which provides tools for defining type annotations, has added a new operator that allows using the syntax 'X | Y' to select one of the types (either type X or type Y). def square(number: int | float) -> int | float: return number ** 2 is equivalent to the previously supported construct: def square(number: Union[int, float]) -> Union[int, float]: return number ** 2
  • The typing module has added the Concatenate operator and the ParamSpec variable, which allow passing additional information for static type checking when using Callable. Special values TypeGuard for annotating type guard functions and TypeAlias for explicitly defining type aliases have also been added to the typing module. StrCache: TypeAlias = 'Cache[str]' # a type alias
  • The zip() function implements an optional flag "strict", which checks for equal lengths of the iterable arguments when specified. >>> list(zip(('a', 'b', 'c'), (1, 2, 3), strict=True)) [('a', 1), ('b', 2), ('c', 3)] >>> list(zip(range(3), ['fee', 'fi', 'fo', 'fum'], strict=True)) Traceback (most recent call last): … ValueError: zip() argument 2 is longer than argument 1
  • New built-in functions aiter() and anext() have been introduced as asynchronous analogs to the iter() and next() functions.
  • The performance of the str(), bytes(), and bytearray() constructors has been improved by 30-40% when working with small objects.
  • The number of import operations in the runpy module has been reduced. The command "python3 -m module_name" now runs on average 1.4 times faster due to a reduction in imported modules from 69 to 51.
  • The LOAD_ATTR instruction uses a mechanism for caching individual opcodes, which has increased the performance of working with regular attributes by up to 36% and with slots by up to 44%.
  • When building Python with the "--enable-optimizations" option, the "-fno-semantic-interposition" mode is now enabled, allowing interpreter performance to improve by up to 30% compared to the build with the "--enable-shared" option.
  • The hashlib and ssl modules have added support for OpenSSL 3.0.0 and have dropped support for OpenSSL versions older than 1.1.1.
  • The old parser has been removed, replaced by the PEG (Parsing Expression Grammar) parser introduced in the previous branch. The formatter module has been removed. The loop parameter has been removed from the asyncio API. Methods that were previously marked as deprecated have been removed. The Py_UNICODE_str* functions, which manipulated Py_UNICODE* strings, have been removed.
  • The distutils module has been deprecated and is planned for removal in Python 3.12. It is recommended to use the setuptools, packaging, platform, shutil, subprocess, and sysconfig modules instead. The wstr structure has been deprecated and is set for removal in PyUnicodeObject.

Source: opennet.ru

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster