Python 3.7 有什麼新功能¶
- 編輯者:
Elvis Pranskevichus <elvis@magic.io>
本文介紹了 Python 3.7 與 3.6 相比多了哪些新功能。Python 3.7 已於 2018 年 6 月 27 日發布。有關完整詳細資訊,請參閱 changelog。
發布重點摘要¶
新增語法特性:
PEP 563, postponed evaluation of type annotations.
Backwards incompatible syntax changes:
新的函式庫模組:
新的內建功能:
PEP 553, the new
breakpoint()function.
Python 資料模型改進:
PEP 562, customization of access to module attributes.
PEP 560, core support for typing module and generic types.
the insertion-order preservation nature of dict objects has been declared to be an official part of the Python language spec.
標準函式庫中的顯著改進
The
asynciomodule has received new features, significant usability and performance improvements.The
timemodule gained support for functions with nanosecond resolution.
CPython 實作改進:
Avoiding the use of ASCII as a default text encoding:
PEP 552, deterministic .pycs
PEP 565, improved
DeprecationWarninghandling
C API 改進:
PEP 539, new C API for thread-local storage
Documentation improvements:
PEP 545, Python documentation translations
New documentation translations: Japanese, French, and Korean.
This release features notable performance improvements in many areas. The 最佳化 section lists them in detail.
For a list of changes that may affect compatibility with previous Python releases please refer to the 移植至 Python 3.7 section.
新增功能¶
PEP 563:延後評估註釋¶
The advent of type hints in Python uncovered two glaring usability issues with the functionality of annotations added in PEP 3107 and refined further in PEP 526:
annotations could only use names which were already available in the current scope, in other words they didn't support forward references of any kind; and
annotating source code had adverse effects on startup time of Python programs.
Both of these issues are fixed by postponing the evaluation of
annotations. Instead of compiling code which executes expressions in
annotations at their definition time, the compiler stores the annotation
in a string form equivalent to the AST of the expression in question.
If needed, annotations can be resolved at runtime using
typing.get_type_hints(). In the common case where this is not
required, the annotations are cheaper to store (since short strings
are interned by the interpreter) and make startup time faster.
Usability-wise, annotations now support forward references, making the following syntax valid:
class C:
@classmethod
def from_string(cls, source: str) -> C:
...
def validate_b(self, obj: B) -> bool:
...
class B:
...
Since this change breaks compatibility, the new behavior needs to be enabled
on a per-module basis in Python 3.7 using a __future__ import:
from __future__ import annotations
這將在 Python 3.10 中成為預設值。
也參考
- PEP 563 -- Postponed evaluation of annotations
PEP written and implemented by Łukasz Langa.
PEP 538: Legacy C Locale Coercion¶
An ongoing challenge within the Python 3 series has been determining a sensible default strategy for handling the "7-bit ASCII" text encoding assumption currently implied by the use of the default C or POSIX locale on non-Windows platforms.
PEP 538 updates the default interpreter command line interface to
automatically coerce that locale to an available UTF-8 based locale as
described in the documentation of the new PYTHONCOERCECLOCALE
environment variable. Automatically setting LC_CTYPE this way means that
both the core interpreter and locale-aware C extensions (such as
readline) will assume the use of UTF-8 as the default text encoding,
rather than ASCII.
The platform support definition in PEP 11 has also been updated to limit full text handling support to suitably configured non-ASCII based locales.
As part of this change, the default error handler for stdin and
stdout is now surrogateescape (rather than strict) when
using any of the defined coercion target locales (currently C.UTF-8,
C.utf8, and UTF-8). The default error handler for stderr
continues to be backslashreplace, regardless of locale.
Locale coercion is silent by default, but to assist in debugging potentially
locale related integration problems, explicit warnings (emitted directly on
stderr) can be requested by setting PYTHONCOERCECLOCALE=warn.
This setting will also cause the Python runtime to emit a warning if the
legacy C locale remains active when the core interpreter is initialized.
While