Python 3.12: Important New Features That You Should Know

Python 3.12 has been released with some new features. In this article, we will discuss some important features of Python 3.12.

1. Improved Error Messages

Python 3.12 introduced some new improved error messages, for example, “Did you” type error messages. More informative easy to easy-to-understand error messages.

Improved error message

Another example considers the following Person class, the calculate method has an “age” variable :

class Person:
   def __init__(self):
       self.age = 30

   def calculate(self):
       temp = age

Person().calculate() # Calling the calculate method 

When we execute the above it will generate a nicely formatted “Did you ” error message as shown below.

Improved import error message

2. Improved syntax error messages when importing modules

Sometimes it is common to write “import x from y” (the correct would be “from y import x“), but this incorrect import will raise a nice syntax error as shown below.

Improved syntax error message

3. Improved import errors

If we are importing a function/class from a module if the imported function/class name is not correct it will suggest a similar function/class name exists in the module. See the below example, we are importing “listdir” from the os module but the import statement has a typo “from os import lstdir“. The import error shows “Did you” error.

Improved import error message

4. The f-string improvements

Python 3.12 removes some of the old restrictions in the f-string. One interesting thing is “quote reuse”.

fruits = ["apple","orange","grapes"]
f"Fruits are {",".join(fruits)}"


Fruits are apple,orange,grapes

Here the “Fruits are {“,”.join(fruits)}” quotes are reused older version of Python does not support this.

5. New override decorator

The typing module introduced a new override decorator. It can used to indicate overridden methods in derived classes.

from typing import override

class Base:
    def display(self) -> None:

class Sub(Base):
    def display(self) -> None:  # Okay: overrides Base.display

    def done(self) -> None:  # Error reported by type checker

6. Buffer protocol accessible in Python

The buffer protocol in Python allows direct access to the memory of an object. It provides a way to access data more efficiently by avoiding unnecessary copying. Classes that implement the __buffer__() method are now usable as buffer types.

The buffer objects can be represented by ABC.

7. Standard library deprecations

The distutils standard library is removed instead we can use the setuptools. The distutils is a module that provides support for building and distributing Python packages. It is recommended to use setuptools for packaging and distribution tasks.

8. Type Parameter Syntax

Python 3.12 introduces a new compact way to create generic classes/functions. Earlier it was more verbose(PEP 484).

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

As we can see it is using the more compact declaration of generic function.

9. The type statement

Python 3.12 introduces a new “type” statement to declare type aliases. For example, a Point type can be declared using the type statement as shown below.

type Point = tuple[float, float]

We can also write type alias using generic type alias.

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


There you have it, these are some important new features of Python 3.12.