What’s New In Python 3.12

编者 :

Adam Turner

This article explains the new features in Python 3.12, compared to 3.11. Python 3.12 was released on October 2, 2023. For full details, see the changelog .

另请参阅

PEP 693 – Python 3.12 Release Schedule

摘要 – 发行亮点

Python 3.12 is a stable release of the Python programming language, with a mix of changes to the language and the standard library. The library changes focus on cleaning up deprecated APIs, usability, and correctness. Of note, the distutils package has been removed from the standard library. Filesystem support in os and pathlib has seen a number of improvements, and several modules have better performance.

The language changes focus on usability, as f-strings have had many limitations removed and ‘Did you mean …’ suggestions continue to improve. The new type parameter syntax and type statement improve ergonomics for using 一般类型 and type aliases with static type checkers.

This article doesn’t attempt to provide a complete specification of all new features, but instead gives a convenient overview. For full details, you should refer to the documentation, such as the 库参考 and 语言参考 . If you want 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.


新句法特征:

New grammar features:

解释器改进:

Python data model improvements:

Significant improvements in the standard library:

Security improvements:

  • Replace the builtin hashlib implementations of SHA1, SHA3, SHA2-384, SHA2-512, and MD5 with formally verified code from the HACL* project. These builtin implementations remain as fallbacks that are only used when OpenSSL does not provide them.

C API 改进:

CPython 实现改进:

  • PEP 709 , comprehension inlining

  • CPython support for the Linux perf profiler

  • Implement stack overflow protection on supported platforms

新类型特征:

Important deprecations, removals or restrictions:

  • PEP 623 :移除 wstr from Unicode objects in Python’s C API, reducing the size of every str object by at least 8 bytes.

  • PEP 632 : Remove the distutils package. See the migration guide for advice replacing the APIs it provided. The third-party Setuptools package continues to provide distutils , if you still require it in Python 3.12 and beyond.

  • gh-95299 : Do not pre-install setuptools in virtual environments created with venv 。这意味着 distutils , setuptools , pkg_resources ,和 easy_install will no longer available by default; to access these run pip install setuptools activated virtual environment.

  • The asynchat , asyncore ,和 imp modules have been removed, along with several unittest.TestCase method aliases .

新特征

PEP 695: Type Parameter Syntax

Generic classes and functions under PEP 484 were declared using a verbose syntax that left the scope of type parameters unclear and required explicit declarations of variance.

PEP 695 introduces a new, more compact and explicit way to create generic classes and 函数 :

def max[T](args: Iterable[T]) -> T:
    ...
class list[T]:
    def __getitem__(self, index: int, /) -> T:
        ...
    def append(self, element: T) -> None:
        ...