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:
Over time, Python has made error messages increasingly accurate and informative. Python 3.12 takes this a step further by:
NameError
suggestions to include ‘self.’ for variables within class instances, addressing a common error source.F-strings in Python used to have many restrictions, which Python 3.12 largely eliminates. Changes include:
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
.
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.
Python 3.12 adds new special methods that allow Python code to interface directly with the buffer protocol, facilitating easier manipulation of data buffers.
Python's type-hinting system continues to evolve. Python 3.12 introduces:
Though Python 3.12 doesn't bring groundbreaking performance improvements, it does offer:
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.
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.
Python 3.12 also brings:
Feel free to test Python 3.12 and explore these enhancements!