The New Interactive Interpreter and REPL Experience Python 3.13 introduces a transformed Read-Eval-Print Loop (REPL) that feels significantly more modern. The most immediate change involves color support; tracebacks and error messages now highlight specific syntax errors in vivid colors, making it faster to identify missing brackets or invalid arguments. Beyond aesthetics, the interpreter provides smarter suggestions. If you mistype a keyword argument like `maxsplit` as `MaxSplit`, the Python interpreter explicitly suggests the correct alternative. Furthermore, the REPL now supports direct commands. You no longer need to call `exit()`, `quit()`, or `help()` as functions with parentheses. Simply typing the word executes the command. For those pasting large code blocks, the new "paste mode" (toggled with F3) prevents the interpreter from misinterpreting indentation during the transfer, a common headache in previous versions. Refined Typing and Module Enhancements Static typing continues to evolve with the inclusion of PEP 705, which adds a `ReadOnly` qualifier for `TypedDict`. This allows developers to explicitly mark dictionary items as immutable for type checkers, though Python still ignores these at runtime. ```python from typing import TypedDict, ReadOnly class Point2D(TypedDict): x: float y: float label: ReadOnly[str] p = Point2D(x=1.1, y=2.2, label="Origin") Type checkers will flag the next line: p["label"] = "New Label" ``` The standard library also saw the removal of 19 "dead batteries"—deprecated modules like `crypt`, `telnetlib`, and `chunk`. Meanwhile, the `math` module gained a `fused multiply-add` (FMA) operation. `math.fma(x, y, z)` computes `(x * y) + z` with a single rounding step, preserving precision that is usually lost in intermediate calculations. Incremental Garbage Collection and Performance Foundations Python 3.13 revamps the cycle garbage collector to be incremental. Previously, a garbage collection cycle could pause the entire program (stop-the-world) to clear circular references. By performing this work in smaller increments, the interpreter reduces maximum pause times by an order of magnitude, especially for applications with large memory heaps. This version also introduces an experimental Just-In-Time (JIT) compiler. When enabled via the `--enable-experimental-jit` build flag, it converts some bytecode into machine code. While current benchmarks show negligible speedups—and even some regressions in I/O heavy tasks—this JIT serves as the structural foundation for massive performance leaps in future releases like Python 3.14. The Free-Threaded Interpreter and GIL Removal A separate executable now offers a "free-threaded" mode, allowing you to disable the Global Interpreter Lock. This is a monumental shift for multi-core processing. In standard Python, the GIL prevents multiple threads from executing Python bytecode simultaneously. With the GIL disabled, multi-threaded CPU-bound tasks can finally run in true parallel. However, this comes with a trade-off. Single-threaded code currently runs slightly slower in the free-threaded version due to the overhead of new thread-safety mechanisms. Developers must decide if the parallel throughput justifies the single-core performance hit. It remains an experimental feature, requiring specific installation (e.g., `3.13.0t` via pyenv), but it marks the beginning of a thread-safe, multi-core future for the language.
PEP 705
Products
- Oct 11, 2024