What’s New In Python 3.13

编辑器 :

Adam Turner and Thomas Wouters

This article explains the new features in Python 3.13, compared to 3.12. Python 3.13 was released on October 7, 2024. For full details, see the changelog .

另请参阅

PEP 719 – Python 3.13 Release Schedule

摘要 – 发行亮点

Python 3.13 is the latest stable release of the Python programming language, with a mix of changes to the language, the implementation and the standard library. The biggest changes include a new interactive interpreter , experimental support for running in a free-threaded mode ( PEP 703 ),和 Just-In-Time compiler ( PEP 744 ).

Error messages continue to improve, with tracebacks now highlighted in color by default. The locals() builtin now has defined semantics for changing the returned mapping, and type parameters now support default values.

The library changes contain removal of deprecated APIs and modules, as well as the usual improvements in user-friendliness and correctness. Several legacy standard library modules have now been removed following their deprecation in Python 3.11 ( PEP 594 ).

This article doesn’t attempt to provide a complete specification of all new features, but instead gives a convenient overview. For full details refer to the documentation, such as the 库参考 and 语言参考 . To understand the complete implementation and design rationale for a change, refer to the PEP for a particular new feature; but note that PEPs usually are not kept up-to-date once a feature has been fully implemented. See Porting to Python 3.13 for guidance on upgrading from earlier versions of Python.


解释器改进:

Python data model improvements:

Significant improvements in the standard library:

Security improvements:

C API 改进:

新类型特征:

Platform support:

Important removals:

  • PEP 594 : The remaining 19 “dead batteries” (legacy stdlib modules) have been removed from the standard library: aifc , audioop , cgi , cgitb , chunk , crypt , imghdr , mailcap , msilib , nis , nntplib , ossaudiodev , pipes , sndhdr , spwd , sunau , telnetlib , uu and xdrlib .

  • Remove the 2to3 tool and lib2to3 module (deprecated in Python 3.11).

  • Remove the tkinter.tix module (deprecated in Python 3.6).

  • Remove the locale.resetlocale() 函数。

  • Remove the typing.io and typing.re namespaces.

  • Remove chained classmethod descriptors.

Release schedule changes:

PEP 602 (“Annual Release Cycle for Python”) has been updated to extend the full support (‘bugfix’) period for new releases to two years. This updated policy means that:

  • Python 3.9–3.12 have one and a half years of full support, followed by three and a half years of security fixes.

  • Python 3.13 and later have two years of full support, followed by three years of security fixes.

新特征

A better interactive interpreter

Python now uses a new interactive shell by default, based on code from the PyPy project . When the user starts the REPL from an interactive terminal, the following new features are now supported:

  • Multiline editing with history preservation.

  • Direct support for REPL-specific commands like help , exit ,和 quit , without the need to call them as functions.

  • Prompts and tracebacks with color enabled by default .

  • Interactive help browsing using F1 with a separate command history.

  • History browsing using F2 that skips output as well as the >>> and prompts.

  • “Paste mode” with F3 that makes pasting larger blocks of code easier (press F3 again to return to the regular prompt).

To disable the new interactive shell, set the PYTHON_BASIC_REPL environment variable. For more on interactive mode, see 交互模式 .

(Contributed by Pablo Galindo Salgado, Łukasz Langa, and Lysandros Nikolaou in gh-111201 based on code from the PyPy project. Windows support contributed by Dino Viehland and Anthony Shaw.)

Improved error messages

  • A common mistake is to write a script with the same name as a standard library module. When this results in errors, we now display a more helpful error message:

    $ python random.py
    Traceback (most recent call last):
      File "/home/me/random.py", line 1, in <module>
        import random
      File "/home/me/random.py", line 3, in <module>
        print(random.randint(5))
              ^^^^^^^^^^^^^^
    AttributeError: module 'random' has no attribute 'randint' (consider renaming '/home/me/random.py' since it has the same name as the standard library module named 'random' and prevents importing that standard library module)