這篇文章給大家分享的是有關(guān)Python 3.9主要的新功能是什么的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。
Python 3.9,來(lái)了!
過(guò)去一年,來(lái)自世界各地的開發(fā)者們一直在致力于Python3.8的改進(jìn)。Python 3.9 beta版本已經(jīng)存在了一段時(shí)間,第一個(gè)正式版本于2020年10月5日發(fā)布。
每個(gè)Python版本都包含新開發(fā)和改進(jìn)的功能,Python 3.9也不例外。
下面介紹Python 3.9幾個(gè)主要的新功能。
字典是Python中最基礎(chǔ)的數(shù)據(jù)結(jié)構(gòu)之一,并且隨著python版本的迭代,性能得到不斷地優(yōu)化。
Python3.9中,合并(|
)和更新(|=
)運(yùn)算符已添加到dict
類中。這些更新完善了現(xiàn)有的dict.update
和{** d1,** d2}
方法。
傳統(tǒng)合并字典的方法:
>>> 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'}復(fù)制代碼
這兩種方法都合并了字典而不更改原始數(shù)據(jù)。請(qǐng)注意,字典1中“Cleveland”已被合并的字典2中“Edinburgh”覆蓋。
你也可以更新字典1:
>>> pycon.update(europython)>>> pycon{2016: 'Portland', 2018: 'Edinburgh', 2017: 'Rimini', 2019: 'Basel'}復(fù)制代碼
新版本的Python引入了兩個(gè)新的字典運(yùn)算符:合并(|
)和更新(|=
)。你可以使用|
合并兩個(gè)字典,而|=
用于更新字典:
>>> 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'}復(fù)制代碼
d1|d2
和{** d1,** d2}
的作用類似,都用于合并字典取并集,遇到相同key,后者會(huì)將前者覆蓋。
使用|
的優(yōu)勢(shì)之一是它適用于類似字典的類型,并在合并后保持原來(lái)的類型:
>>> 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'}復(fù)制代碼
|=
的作用是更新字典,類似于.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'}復(fù)制代碼
|=
還可以將類似字典的數(shù)據(jù)結(jié)構(gòu)用于更新:
>>> 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'}復(fù)制代碼
在Python 3.9中,可以使用.removeprefix
和.removesuffix
分別刪除字符串的開頭或結(jié)尾:
>>> "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'復(fù)制代碼
有人會(huì)說(shuō).strip
方法也可以呀,但是該方法會(huì)出現(xiàn)誤刪操作:
>>> "three cool features in Python".strip(" Python")'ree cool features i'復(fù)制代碼
可以看到,明明想刪掉結(jié)尾的單詞python,但是開頭的there也被刪除了一部分-Th。
所以.removeprefix
和.removesuffix
可能更精準(zhǔn)一些。
zoneinfo是python3.9新引入的模塊,zoneinfo可以訪問(wèn)Internet號(hào)碼分配機(jī)構(gòu)(IANA)時(shí)區(qū)數(shù)據(jù)庫(kù)。IANA每年都會(huì)多次更新其數(shù)據(jù)庫(kù),這是時(shí)區(qū)信息的最權(quán)威來(lái)源。
使用zoneinfo,可以獲得數(shù)據(jù)庫(kù)中描述任何時(shí)區(qū)的對(duì)象:
>>> from zoneinfo import ZoneInfo>>> ZoneInfo("America/Vancouver")zoneinfo.ZoneInfo(key='America/Vancouver') >>> from zoneinfo import ZoneInfo>>> from datetime import datetime, timedelta>>> # 夏令時(shí)>>> dt = datetime(2020, 10, 31, 12, tzinfo=ZoneInfo("America/Los_Angeles"))>>> print(dt)2020-10-31 12:00:00-07:00>>> dt.tzname'PDT'>>> # 標(biāo)準(zhǔn)時(shí)間>>> dt += timedelta(days=7)>>> print(dt)2020-11-07 12:00:00-08:00>>> print(dt.tzname)PST復(fù)制代碼
在類型提示中,現(xiàn)在可以將內(nèi)置集合類型(例如list和dict)用作泛型類型,而不必從typing
中導(dǎo)入相應(yīng)的大寫類型(例如List或Dict)。
def greet_all(names: list[str]) -> None:for name in names:print("Hello", name)復(fù)制代碼
Python 3.9添加了一個(gè)新的模塊graphlib,其中包含graphlib.TopologicalSorter
類,以提供執(zhí)行拓?fù)渑判虻墓δ堋?/p>
>>> dependencies = {... "realpython-reader": {"feedparser", "html2text"},... "feedparser": {"sgmllib3k"},... }...>>> from graphlib import TopologicalSorter>>> ts = TopologicalSorter(dependencies)>>> list(ts.static_order)['html2text', 'sgmllib3k', 'feedparser', 'realpython-reader']復(fù)制代碼
Python長(zhǎng)期以來(lái)一直具有用于計(jì)算兩個(gè)數(shù)字的較大公約數(shù)(GCD)的功能:
>>> import math>>> math.gcd(49, 14)7復(fù)制代碼
最小公倍數(shù)(LCM)與較大公約數(shù)(GCD)有關(guān),可以根據(jù)GCD定義LCM:
>>> def lcm(num1, num2):... if num1 == num2 == 0:... return 0... return num1 * num2 // math.gcd(num1, num2)...>>> lcm(49, 14)98復(fù)制代碼
在Python 3.9中,不再需要定義自己的LCM函數(shù),它新增了計(jì)算最小公倍數(shù)功能:
>>> import math>>> math.lcm(49, 14)98復(fù)制代碼
Python 3.9最酷的功能之一是大家在日常編程中不會(huì)注意到的功能,那就是解析器的更新。解析器是Python解釋器的基本組件。在新版本中,解析器已重新構(gòu)建。
Python之前一直使用LL(1)解析器將源代碼解析為解析樹。你可以將LL(1)解析器視為一次讀取一個(gè)字符,并解釋源代碼而無(wú)需回溯的解析器。
新解釋器是基于PEG(parsing expression grammar)實(shí)現(xiàn)的,并非LL(1)。新解析器的性能可以與舊解析器媲美,在設(shè)計(jì)新語(yǔ)言功能時(shí),PEG比LL(1)更靈活。
在整個(gè)標(biāo)準(zhǔn)庫(kù)中,PEG解析器稍快一些,然而也使用了更多的內(nèi)存。實(shí)際上,使用新解析器時(shí),很難能感知到性能的好壞。
感謝各位的閱讀!關(guān)于Python 3.9主要的新功能是什么就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
當(dāng)前文章:Python3.9主要的新功能是什么-創(chuàng)新互聯(lián)
本文鏈接:http://www.chinadenli.net/article22/igpjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、網(wǎng)站制作、品牌網(wǎng)站制作、自適應(yīng)網(wǎng)站、企業(yè)網(wǎng)站制作、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)