What's new in Python 3.12

Annually, the Python programming language introduces new versions. The first half of the year usually sees a beta release with feature-locked improvements, while the final release comes out by year-end.

Python 3.12 is the latest addition to the Python family. Developers are urged to test this version with non-critical code to check its compatibility and explore new features and performance upgrades.

Let's dive into the key improvements in Python 3.12 that will interest Python developers:

Enhanced Error Messages

Over time, Python has made error messages increasingly accurate and informative. Python 3.12 takes this a step further by:

  • Suggesting standard library modules you might have forgotten to import.
  • Offering better error hints for common syntax mistakes during imports.
  • Making import error messages more helpful by suggesting alternatives within the module's namespace.
  • Extending NameError suggestions to include ‘self.’ for variables within class instances, addressing a common error source.

More Flexible F-strings

F-strings in Python used to have many restrictions, which Python 3.12 largely eliminates. Changes include:

  • F-strings can now contain any valid Python expression.
  • They now allow the same type of quotes used to define the f-string itself.
  • Support for multi-line expressions.
  • The ability to use backslashes and Unicode character definitions.
  • More precise error localization within f-string expressions.

Linux perf Profiler Integration

While the Linux perf profiler could always work with Python, it only provided C-level insights. Python 3.12 allows perf to collect Python-specific data through an opt-in feature, enabled either at the environment level or within the Python code using

sys.activate_stack_trampoline.

Improved Debugging and Profiling Speed

Debuggers and profilers tend to slow down code execution. PEP 669 introduces hooks for events like function starts and ends, allowing for more efficient monitoring.

Buffer Protocol Updates

Python 3.12 adds new special methods that allow Python code to interface directly with the buffer protocol, facilitating easier manipulation of data buffers.

Advances in Type Hinting

Python's type-hinting system continues to evolve. Python 3.12 introduces:

  • The use of TypedDict for keyword argument types in functions.
  • A cleaner syntax for specifying type parameters in generic classes, functions, or type aliases.

Incremental Performance Gains

Though Python 3.12 doesn't bring groundbreaking performance improvements, it does offer:

  • Faster comprehension inlining.
  • "Immortal" objects to improve reference count efficiency.
  • Reduced object sizes for better memory usage and cache locality.

Preliminary Subinterpreter Support

Python 3.12 lays the groundwork for subinterpreters but lacks an end-user interface for them. This functionality is expected to arrive in Python 3.13.

Refinements in Garbage Collection

Previously, Python's garbage collection could activate any time an object was allocated. In Python 3.12, garbage collection is triggered only during the "eval breaker" phase in the Python bytecode loop, which occurs between the execution of one bytecode and the next. The garbage collection process also activates whenever the CPython signal-handler-checking mechanism is called. This new approach allows garbage collection to run intermittently even during a long-lasting call to a C extension that's outside the runtime, enhancing the efficiency of memory management.

Other Updates

Python 3.12 also brings:

  • An unstable API tier, mainly for low-level tools.
  • Removal of obsolete standard library modules like distutils, asynchat, asyncore, and smtpd.
  • Changes to garbage collection timing, allowing for more efficient memory management.

Feel free to test Python 3.12 and explore these enhancements!

Unable to decide?Don’t Worry We’ll Help YouI need help