Towards AI Can Help your Team Adopt AI: Corporate Training, Consulting, and Talent Solutions.


What’s New in Python 3.12 — Features, Updates and Enhancements
Latest   Machine Learning

What’s New in Python 3.12 — Features, Updates and Enhancements

Last Updated on November 5, 2023 by Editorial Team

Author(s): Muttineni Sai Rohith

Originally published on Towards AI.

Python, a dynamic language that is known for its readability, versatility, and strong community, has made its mark once again with the new version 3.12 released on October — 2nd. With numerous enhancements and new features, contributors worldwide helped Python take another giant leap forward. If you are curious about the latest enhancements in this language, then this article is for you.

Python releases new versions yearly, with a feature—locked beta version at the beginning of the year and an official new version at the end of the year.

Python versions

Without wasting time let’s look at the advancements in this version.

Improved Error Messages

Python started giving users suggestions for potential typos, something which is due in this language. Along with that, here are some new features —

  • Modules that we forget to import in our code are suggested in the new version as a part of the error message.
  • In the case of the variable or function name typos, in the NameError suggestions are provided
  • ImportError exceptions raised from failed from <module> import <name> statements now include suggestions for the value of <name> based on the potentially available names in <module>.
  • Syntax error messages have improved when the user types import x from yinstead of from x import y.
  • NameError suggestions now also include self. prepended to the name when raised inside a class instance (e.g., name 'name' is not defined. Did you mean ''?). This is because omitting self , for instance, variables is a common source of errors in class instances.

F-String Enhancements

F-Strings(Formatted Strings) were first introduced in Python 3.6 They are Python’s convenient way for string formatting. In previous versions, they were heavily restricted in how they could be formatted. But in 3.12, few restrictions were lifted.

  • We can now use the string quotes (“ or ‘) inside the F-string, independent of previous usages.
Using Previous versions
Using 3.12 version
  • F-strings can now be multiline expressions similar to Other parentheses or quotes (e.g., using parentheses to allow expressions to span multiple lines).
  • Until now, Backslash(\) or Unicode characters are not allowed in F-Strings, but the restriction is lifted.
Previous Version
Version 3.12
  • Errors within f-string expressions now yield the exact location of the error within the enclosing statement, not just within the expression itself. This makes f-string errors easier to track down and troubleshoot.

Improvement in Typing

Python Type-Hinting Syntax is introduced in Python3.5, which allows linting tools to catch errors ahead of time, TypeDict is the new addition.

from Typing import TypedDict, Unpack

class Employee(TypedDict):
name: str
id: int

def retrieve(**kwargs: Unpack[Employee]) -> None: ...

TypeDict is used to hint at the keyword arguments. Unpack added in 3.11 is used then to unpack the TypeDict.

For generic functions where we can return different datatypes as output, Type variable is introduced for hinting.

def first[T](elements: list[T]) -> T:
return elements[0]

In the above function, if a list of integers is passed, Output will be an integer. If we pass a list of strings, the output will be a String. Also, note that we don’t need to declare the Typing in the above example.

Static type checking has also been enhanced in the recent version. For methods in child classes overriding the parent methods, we can use @override to allow type checkers to detect mistakes.

from typing import override

class Base:
def get_color(self) -> str:
return "blue"
class GoodChild(Base):
@override # Okay: overrides Base.get_color
def get_color(self) -> str:
return "yellow"

Comprehension inlining

Comprehensions, a syntax that lets you quickly construct lists, dictionaries, and sets, will no longer create a single-use function object; they are now constructed “inline”. Resultant speed is now clocked at around 11% for a real-world case and up to twice as fast for a micro-benchmark.

Quickening now happens faster than 3.11. Quickening is the process of noticing that a certain bytecode is executed several times, making it a candidate for Specialization, while specialization means that the interpreter replaces a general bytecode with a specialized one.


They provide the ability to have multiple instances of interpreter, each with its own GIL, running side-by-side within a single Python process. This would be a big step toward better parallelism in Python. However, version 3.12 only includes the CPython internals to make this possible. There’s still no end-user interface for sub-interpreters.

Support for the Linux perf profiler

The widely used Linux profiler tool perf works with Python but only returns information about what's happening at the C level in the Python runtime. Information about actual Python program functions doesn't show up.

Python 3.12 enables an opt-in mode to allow perf the harvest of details about Python programs, not just the runtime. The opt-in can be done at the environment level or inside a Python program with the sys.activate_stack_trampoline function.

Other Notable Features

  • Immortal Objects are introduced — Their Reference count will not change.
  • Smaller Object sizes — the size of the object is decreased to 96 bytes.
  • Calendar constants for days and months.
  • Few Standard library deprecations and removals like distutils is removed in the recent version.
  • A slew of improvements to async in the recent version.

Attaching the release document, which is used as a reference —

So is it worth upgrading to 3.12?

Happy Learning …

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.

Published via Towards AI

Feedback ↓