Python 3.9 的新功能

编者 :

Łukasz Langa

This article explains the new features in Python 3.9, compared to 3.8. Python 3.9 was released on October 5, 2020. For full details, see the changelog .

另请参阅

PEP 596 - Python 3.9 发行时间表

摘要 – 发行亮点

新句法特征:

  • PEP 584 , union operators added to dict ;

  • PEP 585 , type hinting generics in standard collections;

  • PEP 614 , relaxed grammar restrictions on decorators.

新的内置特征:

  • PEP 616 , string methods to remove prefixes and suffixes.

新的标准库特征:

  • PEP 593 , flexible function and variable annotations;

  • os.pidfd_open() added that allows process management without races and signals.

解释器改进:

  • PEP 573 , fast access to module state from methods of C extension types;

  • PEP 617 , CPython now uses a new parser based on PEG;

  • a number of Python builtins (range, tuple, set, frozenset, list, dict) are now sped up using PEP 590 vectorcall;

  • garbage collection does not block on resurrected objects;

  • 很多 Python 模块 ( _abc , audioop , _bz2 , _codecs , _contextvars , _crypt , _functools , _json , _locale , math , operator , resource , time , _weakref ) now use multiphase initialization as defined by PEP 489;

  • 许多标准库模块 ( audioop , ast , grp , _hashlib , pwd , _posixsubprocess , random , select , struct , termios , zlib ) are now using the stable ABI defined by PEP 384.

新的库模块:

  • PEP 615 , the IANA Time Zone Database is now present in the standard library in the zoneinfo 模块;

  • an implementation of a topological sort of a graph is now provided in the new graphlib 模块。

发行流程变化:

  • PEP 602 ,CPython 采纳年度发行周期。

应该检查代码中的 DeprecationWarning

When Python 2.7 was still supported, a lot of functionality in Python 3 was kept for backward compatibility with Python 2.7. With the end of Python 2 support, these backward compatibility layers have been removed, or will be removed soon. Most of them emitted a DeprecationWarning warning for several years. For example, using collections.Mapping 而不是 collections.abc.Mapping 发射 DeprecationWarning since Python 3.3, released in 2012.

Test your application with the -W default command-line option to see DeprecationWarning and PendingDeprecationWarning , or even with -W error to treat them as errors. Warnings Filter can be used to ignore warnings from third-party code.

Python 3.9 is the last version providing those Python 2 backward compatibility layers, to give more time to Python projects maintainers to organize the removal of the Python 2 support and add support for Python 3.9.

Aliases to 抽象基类 collections module, like collections.Mapping alias to collections.abc.Mapping , are kept for one last release for backward compatibility. They will be removed from Python 3.10.

More generally, try to run your tests in the Python 开发模式 which helps to prepare your code to make it compatible with the next Python version.

Note: a number of pre-existing deprecations were removed in this version of Python as well. Consult the 移除 章节。

新特征

Dictionary Merge & Update Operators

合并 ( | ) 和更新 ( |= ) operators have been added to the built-in dict class. Those complement the existing dict.update and {**d1, **d2} methods of merging dictionaries.

范例:

>>> x = {"key1": "value1 from x", "key2": "value2 from x"}
>>> y = {"key2": "value2 from y", "key3": "value3 from y"}
>>> x | y
{'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'}
>>> y | x
{'key2': 'value2 from x', 'key3': 'value3 from y', 'key1': 'value1 from x'}