首頁  >  文章  >  後端開發  >  終於介紹Python 3.9

終於介紹Python 3.9

coldplay.xixi
coldplay.xixi轉載
2020-11-19 17:21:138568瀏覽

今天python教學欄位介紹Python 3.9。

終於介紹Python 3.9

Python 3.9,來了!

過去一年,來自世界各地的開發者一直在致力於Python3.8的改進。 Python 3.9 beta版本已經存在了一段時間,第一個正式版本於2020年10月5日發布。

每個Python版本都包含新開發和改進的功能,Python 3.9也不例外。

Python 3.9,来了

【python學習交流群組】

下面介紹Python 3.9幾個主要的新功能。

1. 字典(合併&更新)運算子

字典是Python中最基礎的資料結構之一,並且隨著python版本的迭代,效能不斷地優化。在

Python3.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 | africadefaultdict(<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解析器稍快一些,然而也使用了更多的記憶體。實際上,使用新解析器時,很難能感知到效能的好壞。

相關免費學習推薦:python教學(影片)

#

以上是終於介紹Python 3.9的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:juejin.im。如有侵權,請聯絡admin@php.cn刪除