iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐍

Python 3.13 is officially released!

に公開
1

Summary of Python 3.13 New Features and Changes

Python 3.13, the latest stable version of the Python programming language, has been officially released.

https://www.python.org/downloads/

In this article, based on the Python 3.13 Release Notes, I will explain the major changes and new features from the previous version (Python 3.12).

Highlights

In Python 3.13, various changes have been made to the language itself, the implementation, and the standard library. The main changes are as follows:

  • Introduction of a new interactive interpreter: A new interactive shell that significantly improves the user experience is now the default.
  • Experimental support for free-threaded mode (PEP 703): It is now possible to run Python code with the Global Interpreter Lock (GIL) disabled.
  • Addition of a Just-In-Time (JIT) compiler (PEP 744): A JIT compiler aimed at performance improvements has been introduced experimentally.

Furthermore, error messages continue to be improved, and tracebacks are now displayed in color by default. Additionally, the behavior of the locals() built-in function has been clarified, and default values are now supported for type parameters.

Interpreter Improvements

New Interactive Interpreter

A new interactive shell based on code from the PyPy project is now the default. Key features include:

  • Multi-line editing and history preservation: Editing code across multiple lines is now possible, and history is preserved.
  • Support for direct REPL commands: Commands like help, exit, and quit can be used without function calls.
  • Color display: Prompts and tracebacks are displayed in color by default.
  • Interactive help browsing: You can browse help by pressing the F1 key.
  • History browsing: You can browse history (excluding output and prompts) by pressing the F2 key.
  • Paste mode: Pasting large blocks of code is made easier with the F3 key.

To disable the new interactive shell, set the environment variable PYTHON_BASIC_REPL.

Error Message Improvements

  • Colored tracebacks: Tracebacks in the terminal are now displayed in color by default. This can be controlled via environment variables PYTHON_COLORS, NO_COLOR, and FORCE_COLOR.
  • Warnings regarding module name conflicts: More specific error messages are displayed when a user script name conflicts with a standard library or third-party module name.
  • Keyword argument typo detection: When an incorrect keyword argument is passed, suggestions for the correct argument name are displayed.

Free-threaded CPython (PEP 703)

Free-threaded mode, which disables the Global Interpreter Lock (GIL), is now experimentally supported. This allows threads to run in parallel, utilizing multi-core CPUs. However, this feature is still experimental and disabled by default.

  • Usage: Use the special executable provided as python3.13t or python3.13t.exe.
  • Activation: Build from source using the --disable-gil option, or select the free-threaded version in the official installer.
  • Notes: Bugs and decreased single-threaded performance are expected at this stage. Improvements are planned for future releases.

Just-In-Time (JIT) Compiler (PEP 744)

An experimental JIT compiler has been added. Performance gains are currently limited, and it is disabled by default. It is expected to be refined in future releases with the goal of improving performance.

Data Model Improvements

  • Addition of the __static_attributes__ attribute: Stores attribute names accessed via self.X in functions within the class body.
  • Addition of the __firstlineno__ attribute: Records the first line number of the class definition.

Major Changes to the Standard Library

  • Addition of the new exception PythonFinalizationError: This exception is raised when an operation is blocked during finalization.
  • argparse module extensions: Support for deprecating command-line options, positional arguments, and subcommands has been added.
  • base64 module extensions: z85encode() and z85decode() functions have been added, enabling the encoding and decoding of Z85 data.
  • copy module enhancements: The copy.replace() function has been added, supported by many built-in types and classes that define the __replace__() method.
  • New dbm.sqlite3 module: SQLite3 is now used as the default dbm backend.
  • os module extensions: New functions have been added for operating Linux timer notification file descriptors.
  • random module command-line interface: The random module can now be used directly from the command line.

Security Improvements

C API Improvements

  • Explicit GIL support for extension modules: The Py_mod_gil slot is used to indicate that an extension module supports execution with the GIL disabled.
  • Access to system clocks: New PyTime C APIs have been added.
  • Addition of a lightweight mutex PyMutex: A 1-byte lightweight mutex (PyMutex) has been provided.
  • Support for PEP 669 monitoring events: A new set of functions has been added to the C API, allowing for the generation of monitoring events.

New Features in typing

Platform Support

Important Removals

PEP 594: Removal of Dead Standard Library Modules

The following 19 modules have been removed from the standard library (deprecated in Python 3.11):

  • aifc
  • audioop
  • cgi, cgitb
  • chunk
  • crypt
  • imghdr
  • mailcap
  • msilib
  • nis
  • nntplib
  • ossaudiodev
  • pipes
  • sndhdr
  • spwd
  • sunau
  • telnetlib
  • uu
  • xdrlib

If you need the functionality of these modules, consider third-party libraries or alternatives.

Other Removals

  • Removal of the 2to3 tool and lib2to3 module (deprecated in Python 3.11)
  • Removal of the tkinter.tix module (deprecated in Python 3.6)
  • Removal of the locale.resetlocale() function
  • Removal of the typing.io and typing.re namespaces
  • Removal of chained classmethod descriptors

Changes to the Release Schedule

With the update of PEP 602, the full support period (bugfix period) for new releases has been extended to 2 years.

  • Python 3.9–3.12: 1.5 years of full support and 3.5 years of security fixes
  • Python 3.13 and later: 2 years of full support and 3 years of security fixes

Details of New Features

Details of the New Interactive Interpreter

The new interactive shell offers the following advantages:

  • Multi-line code editing is easier, and history is preserved.
  • Standard commands (such as help, exit, quit, etc.) can be used without function calls.
  • Color display improves code readability.
  • You can browse interactive help with the F1 key.
  • You can efficiently refer to past input history with the F2 key.
  • You can switch to paste mode with the F3 key, making it easier to paste large blocks of code.

Details of Error Message Improvements

  • Tracebacks in the terminal are displayed in color by default, making it easier to identify errors.
  • If a script name conflicts with a standard library or third-party module name, a specific error message is displayed, allowing for quick problem resolution.
  • If an incorrect keyword argument is passed, the correct argument name is suggested, facilitating easier debugging.

Other Language Changes

  • Removal of leading whitespace from docstrings: To reduce the size of the bytecode cache, common leading whitespace is now removed from each line of docstrings.
  • Support for lambda expressions and comprehensions in class scopes: Lambda expressions and comprehensions are now supported in annotation scopes within class scopes.
  • Change in handling of the __future__ module via relative imports: Relative imports of the __future__ module no longer enable its special features.
  • Allowing global declarations in except blocks: Global variables used in an else block can now be declared with global within the except block.

Conclusion

Thank you for reading until the end. If there are any deficiencies, please feel free to point them out in the comments.

Note: This article is a copy of my Qiita article.

Discussion

ToyoPToyoP

あなた、すごいわ!
17歳って・・・、将来のコンピューター業界、あなたのような人がいると楽しみですね。

是非、私に不明点等あった際には色々教えていただけたら幸いです。

これからもいっぱい頑張ってください。