Python 3.6 有什麼新功能¶
- 編輯者:
Elvis Pranskevichus <elvis@magic.io>, Yury Selivanov <yury@magic.io>
本文介紹了 Python 3.6 與 3.5 相比多了哪些新功能。Python 3.6 已於 2016 年 12 月 23 日發布。完整詳情請見 changelog。
也參考
PEP 494 - Python 3.6 發佈時程
發布重點摘要¶
新增語法特性:
PEP 498, formatted string literals.
PEP 515, underscores in numeric literals.
PEP 526, syntax for variable annotations.
PEP 525, asynchronous generators.
PEP 530: asynchronous comprehensions.
新的函式庫模組:
CPython 實作改進:
The dict type has been reimplemented to use a more compact representation based on a proposal by Raymond Hettinger and similar to the PyPy dict implementation. This resulted in dictionaries using 20% to 25% less memory when compared to Python 3.5.
Customization of class creation has been simplified with the new protocol.
The class attribute definition order is now preserved.
The order of elements in
**kwargsnow corresponds to the order in which keyword arguments were passed to the function.DTrace and SystemTap probing support has been added.
The new PYTHONMALLOC environment variable can now be used to debug the interpreter memory allocation and access errors.
標準函式庫中的顯著改進
The
asynciomodule has received new features, significant usability and performance improvements, and a fair amount of bug fixes. Starting with Python 3.6 theasynciomodule is no longer provisional and its API is considered stable.A new file system path protocol has been implemented to support path-like objects. All standard library functions operating on paths have been updated to work with the new protocol.
The
datetimemodule has gained support for Local Time Disambiguation.The
typingmodule received a number of improvements.The
tracemallocmodule has been significantly reworked and is now used to provide better output forResourceWarningas well as provide better diagnostics for memory allocation errors. See the PYTHONMALLOC section for more information.
安全性改進:
The new
secretsmodule has been added to simplify the generation of cryptographically strong pseudo-random numbers suitable for managing secrets such as account authentication, tokens, and similar.On 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 default settings and feature set of the
sslmodule have been improved.The
hashlibmodule received support for the BLAKE2, SHA-3 and SHAKE hash algorithms and thescrypt()key derivation function.
Windows 改進:
PEP 528 and PEP 529, Windows filesystem and console encoding changed to UTF-8.
The
py.exelauncher, 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.exeandpythonw.exehave been marked as long-path aware, which means that the 260 character path limit may no longer apply. See removing the MAX_PATH limitation for details.A
._pthfile can be added to force isolated mode and fully specify all search paths to avoid registry and environment lookup. See the documentation for more information.A
python36.zipfile now works as a landmark to inferPYTHONHOME. See the documentation for more information.
新增功能¶
PEP 498: Formatted string literals¶
PEP 498 introduces a new kind of string literals: f-strings, or