Python 3.6 的新功能

编辑器 :

Elvis Pranskevichus < elvis @ magic . io >, Yury Selivanov < yury @ magic . io >

This article explains the new features in Python 3.6, compared to 3.5. Python 3.6 was released on December 23, 2016.  See the changelog for a full list of changes.

另请参阅

PEP 494 - Python 3.6 发行时间表

摘要 – 发行亮点

新句法特征:

新的库模块:

CPython 实现改进:

Significant improvements in the standard library:

Security improvements:

  • 新的 secrets module has been added to simplify the generation of cryptographically strong pseudo-random numbers suitable for managing secrets such as account authentication, tokens, and similar.

  • 在 Linux, os.urandom() now blocks until the system urandom entropy pool is initialized to increase the security. See the PEP 524 for the rationale.

  • The hashlib and ssl modules now support OpenSSL 1.1.0.

  • The default settings and feature set of the ssl module have been improved.

  • The hashlib module received support for the BLAKE2, SHA-3 and SHAKE hash algorithms and the scrypt() key derivation function.

Windows improvements:

  • PEP 528 and PEP 529 , Windows filesystem and console encoding changed to UTF-8.

  • The py.exe launcher, when used interactively, no longer prefers Python 2 over Python 3 when the user doesn’t specify a version (via command line arguments or a config file). Handling of shebang lines remains unchanged - “python” refers to Python 2 in that case.

  • python.exe and pythonw.exe have been marked as long-path aware, which means that the 260 character path limit may no longer apply. See removing the MAX_PATH limitation 了解细节。

  • A ._pth file can be added to force isolated mode and fully specify all search paths to avoid registry and environment lookup. See the documentation 了解更多信息。

  • A python36.zip file now works as a landmark to infer PYTHONHOME 。见 the documentation 了解更多信息。

新特征

PEP 498: 格式化字符串文字

PEP 498 引入一种新的字符串文字: f-strings ,或 格式化字符串文字 .

Formatted string literals are prefixed with 'f' and are similar to the format strings accepted by str.format() . They contain replacement fields surrounded by curly braces. The replacement fields are expressions, which are evaluated at run time, and then formatted using the format() 协议:

>>> name = "Fred"
>>> f"He said his name is {name}."
'He said his name is Fred.'
>>> width = 10
>>> precision = 4
>>> value = decimal.Decimal("12.34567")
>>> f"result: {value:{width}.{precision}}"  # nested fields
'result:      12.35'