Python 3.11 Release Notes
Python 3.11.0, released on October 24, 2022, marks a major leap in performance and developer productivity. It introduces an experimental faster CPython mode, exception groups for handling multiple errors, improved error messages, and the new self type for better static type checking. This version is up to 25% faster than Python 3.10 on average, thanks to a new specializing adaptive bytecode interpreter.
Key goals include speeding up common operations without code changes, enhancing concurrency with exception groups, and improving debugging with clearer tracebacks. Python 3.11 follows the new annual release cycle with 5 years of full support, making it ideal for new projects in web development, data science, automation, and system scripting.
Faster CPython (Experimental)
Python 3.11 includes an experimental optimized mode that runs significantly faster. Enable it with python -X perf or by building with --enable-optimizations. This mode uses a new adaptive interpreter that specializes bytecode at runtime.
Benchmarks show 10-60% speedups in real-world tasks like JSON parsing, template rendering, and numerical loops. The optimization is transparent -- no source changes needed. It's marked experimental but stable enough for production testing.
Specializing Adaptive Interpreter
The core innovation is a new interpreter that monitors execution and replaces generic bytecode with type-specific versions. For example, BINARY_ADD becomes BINARY_ADD_INT when both operands are integers.
Specializations cover arithmetic, comparisons, function calls, attribute access, and container operations. This reduces interpreter overhead dramatically in hot code paths, especially loops and frequently called functions.
Exception Groups and except*
PEP 654 introduces ExceptionGroup and except* to handle multiple exceptions from concurrent tasks. Raise an ExceptionGroup to bundle errors, then catch specific types with except* ErrorType:.
try:
await gather(task1(), task2())
except* ConnectionError as eg:
print(f"Network issues: {eg.exceptions}")
except* ValueError as eg:
print(f"Data issues: {eg.exceptions}")
This is essential for asyncio, concurrent.futures, and any parallel code where multiple failures can occur simultaneously.
Self Type for Instance Methods
PEP 673 adds the Self type from typing to annotate methods that return their instance. This enables precise type checking in method chains:
from typing import Self
class Builder:
def set_name(self, name: str) -> Self:
self.name = name
return self
def build(self) -> "Product":
return Product(self.name)
Static type checkers like mypy now understand fluent interfaces and builder patterns correctly.
Variadic Generics
PEP 646 introduces variadic type variables with *Ts and **P syntax. This allows type-safe handling of heterogeneous tuples and mappings:
def zip_strict[*Ts](*iterables: *Ts) -> list[tuple[*Ts]]: ...
Useful for libraries like NumPy, Pydantic, and any code that processes variable-length type sequences.
Improved Error Messages
Error messages are clearer and more helpful. NameError suggests similar names: NameError: name 'user_name' is not defined. Did you mean: 'username'?. SyntaxError points to the exact location with a caret.
Import errors show the full module path. Unclosed parentheses highlight the opening bracket. These changes reduce debugging time significantly for beginners and experts alike.
Performance Optimizations
Function calls are faster with inline caching. Attribute access uses per-type caches. List and dict literals build quicker. super() without arguments is optimized in single inheritance.
The pyperformance benchmark suite shows a 25% average speedup over Python 3.10. Real-world apps like Django and FastAPI see 15-30% gains in request handling.
Standard Library Enhancements
tomllib: New module for parsing TOML files (PEP 680). typing: LiteralString, Never`, `Required, NotRequired, TypedDict improvements.
math: math.mode() for statistical mode. statistics: covariance(), correlation(). zoneinfo: Updated IANA database.
pathlib: Path.read_text() and write_text() accept errors parameter. asyncio: Task groups, create_task() with name, better cancellation.
Deprecations
distutils is deprecated -- use setuptools or build. asynchat, asyncore, smtpd are deprecated. parser module is deprecated.
Passing non-path objects to Path methods is deprecated. typing.AnyStr is deprecated. wsgiref and cgi are on the removal path.
Removals
Removed: pip from stdlib (use external pip), smtpd.MailmanProxy, asyncio.get_event_loop() without running loop, Py_UNICODE APIs.
Support for Windows 7/8 and macOS 10.9 ended. Old C APIs like PyEval_CallObject are gone. array 'u' type code is removed.
C API Changes
New immortal objects reduce reference counting. Vectorcall protocol is faster. Stable ABI extended. PyCode_New() and friends have new signatures.
C extensions must be recompiled. New PyConfig API for initialization. Private APIs are hidden behind _Py prefix.
Migration and Support
Most Python 3.10 code runs unchanged. Enable warnings with -W default to catch deprecations. Rebuild C extensions. Update code using removed modules.
Full support until October 2027. Use python -X perf to test the faster mode. Update type hints to use Self and variadic generics for better checking.