BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Breaking Down Python 3.13’s Latest Features

Breaking Down Python 3.13’s Latest Features

Python 3.13, the latest major release of the Python programming language is now available. It introduces a revamped interactive interpreter with streamlined features like multi-line editing, enabling users to edit code blocks efficiently by retrieving the entire context with a single key press. Furthermore, Python 3.13 allows for the experimental disabling of the Global Interpreter Lock (GIL), alongside the introduction of a Just-in-Time (JIT) compiler, enhancing performance though still in an experimental phase. Lastly, the update removes several outdated modules and introduces a random.

A new interactive interpreter

Python 3.13 introduces a new interactive interpreter with exciting features, starting with multi-line editing. Previously, pressing the up key would navigate through previous commands line by line, making it challenging to edit multi-line structures like classes or functions. With Python 3.13, pressing up retrieves the entire block of code and recognizes the context, which allows for easier and more efficient editing. Furthermore, by pressing F3, users can enable paste mode and insert chunks of code, including support for multi-line.

In previous versions, users had to type functions like help(), exit(), and quit(). With the new interactive interpreter, users can now just type help or F1, allowing them to browse the Python documentation, including modules. Similarly, for exit() and quit(), just typing the words exit or quit will quit the interactive interpreter. This is a common complaint from programmers new to REPL(Read-Eval-Print Loop) or interactive interpreters.

Developers can now also clear the screen using the clear command in the interpreter. Previous versions of the interpreter did not have this command, and developers had to resort to terminal configurations to achieve this behavior. Lastly, the new color prompts and tracebacks also aid in better usability for the developers.

Free-threaded

It is now possible to disable the Global Interpreter Lock (GIL), an experimental feature disabled by default. The free-threaded mode needs different binaries, e.g., python3.13t. In simplified terms, GIL is a mutex (lock) that controls the Python interpreter. It provides thread safety but also means that only one thread can be in an execution state at a time.

GIL makes many types of parallelism difficult, such as neural networks and reinforcement learning or scientific and numerical calculations where parallelism using CPU and GPU is necessary. Contention issues cause conflicts with a Single-threaded model. Free-threaded mode allows applications to consume all the underlying CPU cores by providing a multi-threading programming model.

Python has been great for single-threaded applications. However, modern languages like Java, Go, and Rust all provide a model for multi-threading, making comprehensive use of the underlying hardware. GIL has provided thread safety for Python applications, e.g. using C libraries. However, removing GIL has been challenging, with failed attempts and broken integration into C extensions. Developers are used to using the Single-threaded model for applications, and disabling GIL can cause unexpected behavior.

The Just-in-Time Compiler

An experimental JIT (Just-in-time) compiler is now part of the main branch in Python. However, in Python 3.13, it is turned off by default. It is not part of the default build configuration and will likely stay that way in the foreseeable future. Users can enable JIT while building CPython by specifying the following flag.

--enable-experimental-jit

The Python team has been adding enhancements in this direction for some time. For example, The Specializing Adaptive Interpreter introduced in Python 3.11 (PEP 659) would rewrite the bytecode instructions in place with type-specialized versions as the program ran. The JIT, however, is a leap toward a much more comprehensive array of performance enhancements.

This experimental JIT uses copy-and-patch compilation, which is a fast compilation algorithm for high-level languages and bytecode. A fast compilation technique that is capable of lowering both high-level languages and low-level bytecode programs to binary code by stitching together code from an extensive library of binary implementation variants. Haoran Xu, Fredrik Kjolstad describe the approach in-depth in this paper.

The current interpreter and JIT are both generated from the same specification, thereby ensuring backward compatibility. According to PEP 744, any observable differences in testing were mostly bugs due to micro-op translation or optimization stages. Furthermore, the JIT is currently developed for all Tier 1 platforms, some Tier 2 platforms, and one Tier 3 platform. The JIT is a move away from how CPython executes Python code. The performance of the JIT is similar to the existing specializing interpreter, while it does bring some overhead, for example, build time dependencies and memory consumption.

Other updates

Python 3.13 removes several modules (PEP 594), and soft deprecates tools like optparse and getopt, and introduces new restrictions on certain built-in functionalities, particularly in C-based libraries.Python 3.13 also introduces a CLI feature for random. Users can now generate random words from a list of words or sentences or generate random floats, decimals, or integer values, by calling the random function as follows.

# random words from a sentence or list of words
python -m random this is a test
python -m random --choice this is a test

# random integers between 1 and 100
python -m random 100
python -m random --integer 100

# random floating-point numbers
python -m random 100.00 
python -m random --float 100

The behavior of locals has also changed in Python 3.13. It now returns an independent snapshot of the local variables and values without affecting any future calls within the same scope. Previously locals() was inconsistent and leading to bugs. This change brings more consistency and helps developers during the debugging process. this change affects locals() and globals() in exec and eval.

def locals_test_func()
    x = 10
    locals()["x"] = 13
    return x

Python 3.13 introduces significant changes that enhance the developer experience. Improvements like the interactive interpreter, along with experimental features like free-threaded and the JIT compiler, promise a more robust and dynamic programming experience. The experimental disabling of GIL enables current libraries and frameworks to adjust and make changes for future releases.

 

About the Author

Rate this Article

Adoption
Style

BT