What’s New In Python 3.12


Written on May 20, 2024

Here is a summary of the key new features in Python 3.12 (https://docs.python.org/3/whatsnew/3.12.html):

PEP 695: Type Parameter Syntax

This introduces a new, more compact and explicit way to create generic classes and functions using type parameter syntax, as well as a new type statement to declare type aliases.

Example:

def max[T](args: Iterable[T]) -> T:
    ...

class list[T]:
    def __getitem__(self, index: int, /) -> T:
        ...

    def append(self, element: T) -> None:
        ...

type Point = tuple[float, float]
type Point[T] = tuple[T, T]

PEP 701: Syntactic Formalization of f-strings

This formalizes f-strings in the Python grammar, which should result in faster tokenization of f-strings.

  • Quote resue:
    songs = ['Take me back to Eden', 'Alkaline', 'Ascensionism']
    f"This is the playlist: {", ".join(songs)}"
    
  • Multi-line expressions and comments:
    >>> f"This is the playlist: {", ".join([
    ...     'Take me back to Eden',  # My, my, those eyes like fire
    ...     'Alkaline',              # Not acid nor alkaline
    ...     'Ascensionism'           # Take to the broken skies at last
    ... ])}"
    'This is the playlist: Take me back to Eden, Alkaline, Ascensionism'
    

PEP 684: A Per-Interpreter GIL

This introduces a unique Global Interpreter Lock (GIL) per interpreter, improving scalability on multi-core systems.

PEP 669: Low Impact Monitoring for CPython

This adds low-impact monitoring capabilities to CPython, providing more insight into the runtime behavior of Python programs.

PEP 688: Making the Buffer Protocol Accessible in Python

This makes the buffer protocol more accessible to Python programmers.

PEP 709: Comprehension Inlining

This optimizes the implementation of list, set, and dictionary comprehensions.

Improved error messages for NameError, ImportError, and SyntaxError exceptions

  • Modules from the standard library are now potentially suggested as part of the error messages:
sys.version_info
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'sys' is not defined. Did you forget to import 'sys'?
  • Now if a NameError is raised in a method and the instance has an attribute that’s exactly equal to the name in the exception, the suggestion will include self. instead of the closest match in the method scope.
class A:
   def __init__(self):
       self.blech = 1

   def foo(self):
       somethin = blech

A().foo()
Traceback (most recent call last):
  File "<stdin>", line 1
    somethin = blech
               ^^^^^
NameError: name 'blech' is not defined. Did you mean: 'self.blech'?

More syntax error

import a.y.z from b.y.z
Traceback (most recent call last):
  File "<stdin>", line 1
    import a.y.z from b.y.z
    ^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Did you mean to use 'from ... import ...' instead?

The release also includes many other improvements and changes across the standard library, optimizations, and deprecations/removals. Notable changes include:

  • Various performance improvements, including in asyncio and the tokenize module
  • Removal of the distutils package (with Setuptools providing compatibility)
  • Removal of several deprecated modules like asynchat, asyncore, and imp

Overall, Python 3.12 focuses on usability improvements, performance enhancements, and cleanup of deprecated APIs, with a particular emphasis on type hinting and generic programming features.