Earlier today the Python release team announced that the newest major release of the language, Python 3.12.0, is now available.
It includes many new features, such as flexible f-string parsing, support for the buffer protocol, a new debugging and profiling API, and more.
F-strings can now include any valid Python expression in their components. According to the release team, this includes strings that reuse the same quote as the f-string, multi-line expressions, comments, backslashes, and unicode escape sequences.
Python 3.12.0 enables use of the buffer protocol, which allows Python code to access information from within their memory buffer. As of this release, any classes that implement the __buffer__() method can be used as buffer types. There is also a new abstract base class (ABC), collections.abc.Buffer, which offers a standard way for representing buffer objects.
Another new addition to this release is a low-cost debugging/profiling API for monitoring in CPython. According to the Python team, using a profiler or debugger typically severely impacts performance and can be expensive, but this new one will keep costs down because you only pay for what you use. It can handle a wide range of events, such as calls, returns, lines, exceptions, and jumps.
Error messages have also been improved in Python 3.12.0. The messages now suggest modules from the standard libraries, the error suggestion for NameError exceptions are now more exact, the SyntaxError message from when a user tries to import things in the wrong direction (ie “import x from y” vs “import y from x”) has been improved, and the ImportError message now include suggestions based on available names in <module>.
Also in Python 3.12.0, dictionary, list, and set comprehensions are inlined to speed up execution time. Previously a new function object needed to be created in order to execute a comprehension.
There were also a few new updates to type hints and typing module. These include relying on typed dictionaries when typing “**kwargs” to allow for more precise typing, and a new decorator for overriding a method.
This release also includes many other small optimizations and deprecations of features. For more information, visit the release notes here.