Python 3.9,来了!

Python 3.9,来了!

过去一年来,来自世界各地的开发者们一直在致力于Python 3.8的改进。Python 3.9 beta版本已经存在了一段时间,第一个正式版本于2020年10月5日发布。

每个Python版本都包含新开发和改进的功能,Python 3.9也不例外。

下面介绍Python 3.9几个主要的新功能。

1. 字典(合并&更新)运算符

字典是Python中最基础的数据结构之一,并且随着python版本的迭代性能得到不断地优化。

Python 3.9中,合并(|)和更新(|=)运算符已添加到dict类中。这些更新完善了现有的dict.update{** d1,** d2}方法。

传统合并字典的方法:

>>> pycon = {2016: "Portland", 2018: "Cleveland"} # 字典1
>>> europython = {2017: "Rimini", 2018: "Edinburgh", 2019: "Basel"} # 字典2

# 方法一
>>> {**pycon, **europython}
{2016: 'Portland', 2018: 'Edinburgh', 2017: 'Rimini', 2019: 'Basel'}

#方法二
>>> merged = pycon.copy()
>>> for key, value in europython.items():
...     merged[key] = value
...
>>> merged
{2016: 'Portland', 2018: 'Edinburgh', 2017: 'Rimini', 2019: 'Basel'}

这两种方法都合并了字典而不更改原始数据。请注意,字典1中“Cleveland”已被合并的字典2中“Edinburgh”覆盖。

你也可以更新字典1:

>>> pycon.update(europython)
>>> pycon
{2016: 'Portland', 2018: 'Edinburgh', 2017: 'Rimini', 2019: 'Basel'}

新版本的Python引入了两个新的字典运算符:合并(|)和更新(|=)。你可以使用|合并两个字典,而|=用于更新字典:

>>> pycon = {2016: "Portland", 2018: "Cleveland"}
>>> europython = {2017: "Rimini", 2018: "Edinburgh", 2019: "Basel"}

>>> pycon | europython  # 合并
{2016: 'Portland', 2018: 'Edinburgh', 2017: 'Rimini', 2019: 'Basel'}

>>> pycon |= europython # 更新
>>> pycon
{2016: 'Portland', 2018: 'Edinburgh', 2017: 'Rimini', 2019: 'Basel'}

d1|d2{** d1,** d2}的作用类似,都用于合并字典取并集,遇到相同key,后者会将前者覆盖。

使用|的优势之一是它适用于类似于字典的类型,并在合并后保持原来的类型:

>>> from collections import defaultdict
>>> europe = defaultdict(lambda: "", {"Norway": "Oslo", "Spain": "Madrid"})
>>> africa = defaultdict(lambda: "", {"Egypt": "Cairo", "Zimbabwe": "Harare"})

>>> europe | africa
defaultdict(<function <lambda> at 0x7f0cb42a6700>,
  {'Norway': 'Oslo', 'Spain': 'Madrid', 'Egypt': 'Cairo', 'Zimbabwe': 'Harare'})

>>> {**europe, **africa}
{'Norway': 'Oslo', 'Spain': 'Madrid', 'Egypt': 'Cairo', 'Zimbabwe': 'Harare'}

|=的作用是更新字典,类似于.update()

>>> libraries = {
...     "collections": "Container datatypes",
...     "math": "Mathematical functions",
... }
>>> libraries |= {"zoneinfo": "IANA time zone support"}
>>> libraries
{'collections': 'Container datatypes', 'math': 'Mathematical functions',
 'zoneinfo': 'IANA time zone support'}

|=还可以将类似字典的数据结构用于更新:

>>> libraries |= [("graphlib", "Functionality for graph-like structures")]
>>> libraries
{'collections': 'Container datatypes', 'math': 'Mathematical functions',
 'zoneinfo': 'IANA time zone support',
 'graphlib': 'Functionality for graph-like structures'}

2. 删除字符串前缀后缀

在Python 3.9中,可以使用.removeprefix().removesuffix()分别删除字符串的开头或结尾:

>>> "three cool features in Python".removesuffix(" Python")
'three cool features in'

>>> "three cool features in Python".removeprefix("three ")
'cool features in Python'

>>> "three cool features in Python".removeprefix("Something else")
'three cool features in Python'

有人会说.strip方法也可以呀,但是该方法会出现误删操作:

>>> "three cool features in Python".strip(" Python")
'ree cool features i'

可以看到,明明想删掉结尾的单词python,但是开头的there也被删除了一部分-Th。

所以.removeprefix().removesuffix()可能更精准一些。

3. zoneinfo时区模块

zoneinfo是python3.9新引入的模块,zoneinfo可以访问Internet号码分配机构(IANA)时区数据库。 IANA每年都会多次更新其数据库,这是时区信息的最权威来源。

使用zoneinfo,可以获得数据库中描述任何时区的对象:

>>> from zoneinfo import ZoneInfo
>>> ZoneInfo("America/Vancouver")
zoneinfo.ZoneInfo(key='America/Vancouver')
>>> from zoneinfo import ZoneInfo
>>> from datetime import datetime, timedelta

>>> # 夏令时
>>> dt = datetime(2020, 10, 31, 12, tzinfo=ZoneInfo("America/Los_Angeles"))
>>> print(dt)
2020-10-31 12:00:00-07:00
>>> dt.tzname()
'PDT'

>>> # 标准时间
>>> dt += timedelta(days=7)
>>> print(dt)
2020-11-07 12:00:00-08:00
>>> print(dt.tzname())
PST

4. 内置集合用于类型提示

在类型提示中,你现在可以将内置集合类型(例如list和dict)用作泛型类型,而不必从typing中导入相应的大写类型(例如List或Dict)。

def greet_all(names: list[str]) -> None:
    for name in names:
        print("Hello", name)

5. 拓扑排序

Python 3.9添加了一个新的模块graphlib,其中包含graphlib.TopologicalSorter类,以提供执行拓扑排序的功能。

>>> dependencies = {
...     "realpython-reader": {"feedparser", "html2text"},
...     "feedparser": {"sgmllib3k"},
... }
...

>>> from graphlib import TopologicalSorter
>>> ts = TopologicalSorter(dependencies)
>>> list(ts.static_order())
['html2text', 'sgmllib3k', 'feedparser', 'realpython-reader']

6. 最小公倍数(LCM)

Python长期以来一直具有用于计算两个数字的最大公约数(GCD)的功能:

>>> import math
>>> math.gcd(49, 14)
7

最小公倍数(LCM)与最大公约数(GCD)有关,可以根据GCD定义LCM:

>>> def lcm(num1, num2):
...     if num1 == num2 == 0:
...         return 0
...     return num1 * num2 // math.gcd(num1, num2)
...
>>> lcm(49, 14)
98

在Python 3.9中,您不再需要定义自己的LCM函数,它新增了计算最小公倍数功能:

>>> import math
>>> math.lcm(49, 14)
98

7. 更强大的Python解析器

Python 3.9最酷的功能之一是大家在日常编程中不会注意到的功能,那就是解析器的更新。解析器是Python解释器的基本组件。在最新版本中,解析器已重新构建。

Python之前一直使用LL(1)解析器将源代码解析为解析树。你可以将LL(1)解析器视为一次读取一个字符,并解释源代码而无需回溯的解析器。

新解释器是基于PEG(parsing expression grammar)实现的,并非LL(1)。新解析器的性能可以与旧解析器媲美,在设计新语言功能时,PEG比LL(1)更灵活。

在整个标准库中,PEG解析器稍快一些,然而也使用了更多的内存。实际上,使用新解析器时,很难能感知到性能的好坏。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章