Python 3.12 Release Notes
Python 3.12.0, released on October 2, 2023, brings significant improvements in performance, error messages, and developer tools. This version introduces faster CPython with a new specializing adaptive interpreter, better f-string debugging, type parameter syntax in generics, and enhanced support for Linux perf profiling. It also removes several deprecated features and modules as part of ongoing modernization efforts.
With over 200 performance optimizations, Python 3.12 runs about 5% faster on average than 3.11, especially in function calls, list operations, and method lookups. It maintains full backward compatibility for most code while setting the stage for future concurrency improvements like free-threaded execution.
Specializing Adaptive Interpreter
The new interpreter dynamically specializes bytecode based on runtime types. It starts with generic instructions and replaces them with faster type-specific versions when patterns are detected -- for example, converting BINARY_ADD to BINARY_ADD_INT when both operands are integers.
This reduces overhead in hot loops and common operations. Specializations cover integers, floats, strings, lists, and method calls. The system is self-tuning and requires no code changes.
Improved f-string Debugging
f-strings now support = for self-documenting expressions. Write f"{expr=}" to include both the expression and its value in output, like expr=42.
This is especially useful in logging and debugging. Nested f-strings and reuse of the = specifier are fully supported, making complex expressions clearer without extra print statements.
Type Parameter Syntax
PEP 695 introduces a new, cleaner syntax for generics using def, class, and type statements. Define type variables directly in function or class scope:
def max[T](a: T, b: T) -> T: ...
This replaces the older from typing import TypeVar pattern, reducing boilerplate and improving readability in type-heavy code.
Linux perf Profiler Support
Python 3.12 adds built-in support for the Linux perf tool. Run python -X perf to generate profiling data that perf report can read, showing exact Python line numbers and function calls.
No source modifications needed. This enables deep performance analysis in production-like environments, identifying bottlenecks at the interpreter level.
Performance Improvements
Function calls are 3-10% faster due to inline caching and reduced frame overhead. List and dict literals build up to 25% quicker. Method calls benefit from per-type caches.
super() without arguments is faster in single inheritance. String concatenation in loops is optimized. Overall, the pyperformance suite shows a 5% average speedup.
Enhanced Error Messages
Error messages are more precise. For example, NameError now suggests "Did you mean: 'other_name'?" when a similar name exists in scope. Import errors show the full path of the failing module.
SyntaxError points to the exact token. Unclosed parentheses or brackets highlight the opening location. These changes help developers fix issues faster.
New Syntax Features
Beyond f-string debugging and type parameters, Python 3.12 allows except* for exception groups (PEP 654), enabling handling of multiple exceptions from concurrent tasks.
Variadic generics with *args: *Ts and **kwargs: **Ps are supported. The type statement defines type aliases cleanly: type Point = tuple[float, float].
Standard Library Updates
pathlib: Path.walk() for recursive traversal. statistics: fmean() supports weights. sqlite3: Faster row factory, iterdump() returns iterator.
os: cpu_count() includes only online CPUs by default. tkinter: Dark mode detection on macOS. zoneinfo: IANA database updated.
dataclasses: KW_ONLY for keyword-only fields. array: 'u' type code for Unicode removed (use str).
Deprecations
wsgiref, smtplib, distutils are deprecated in favor of modern alternatives. asynchat, asyncore, smtpd are removed.
Passing non-path objects to Path methods is deprecated. typing.AnyStr is deprecated. pkgutil.find_loader() and imp module are gone.
Removals
Removed: parser module, smtpd, asynchat, asyncore, distutils, lib2to3, imp module, pip from stdlib.
Old C APIs like PyEval_CallObject are removed. Py_UNICODE APIs are gone. Support for Windows 7/8 and macOS 10.9 ended.
C API Changes
Stable ABI extended. New PyCode_NewEmpty() for error reporting. PyObject_GC_Track() and friends are private.
Immortal objects reduce reference counting overhead. Vectorcall protocol is faster. C extensions must be recompiled.
Migration and Support
Most Python 3.11 code runs unchanged. Enable warnings with -W default to catch deprecations. Rebuild C extensions. Update code using removed modules.
Full support until October 2025, security fixes to October 2028. Use python -m venv for clean environments. Test with pyperformance to measure speed gains.