自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python 3.9,來了!

開發(fā) 前端
Python 3.9,來了!每個(gè)Python版本都包含新開發(fā)和改進(jìn)的功能,Python 3.9也不例外。

Python 3.9,來了!

過去一年,來自世界各地的開發(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è)主要的新功能。

1. 字典(合并&更新)運(yùn)算符

字典是Python中最基礎(chǔ)的數(shù)據(jù)結(jié)構(gòu)之一,并且隨著python版本的迭代,性能得到不斷地優(yōu)化。

Python3.9中,合并(|)和更新(|=)運(yùn)算符已添加到dict類中。這些更新完善了現(xiàn)有的dict.update和{** d1,** d2}方法。

傳統(tǒng)合并字典的方法:

  1. >>> pycon = {2016: "Portland", 2018: "Cleveland"} # 字典1 
  2. >>> europython = {2017: "Rimini", 2018: "Edinburgh", 2019: "Basel"} # 字典2 
  3.  
  4. # 方法一 
  5. >>> {**pycon, **europython} 
  6. {2016: 'Portland', 2018: 'Edinburgh', 2017: 'Rimini', 2019: 'Basel'} 
  7.  
  8. #方法二 
  9. >>> merged = pycon.copy() 
  10. >>> for key, value in europython.items(): 
  11. ...     merged[key] = value 
  12. ... 
  13. >>> merged 
  14. {2016: 'Portland', 2018: 'Edinburgh', 2017: 'Rimini', 2019: 'Basel'} 

這兩種方法都合并了字典而不更改原始數(shù)據(jù)。請注意,字典1中“Cleveland”已被合并的字典2中“Edinburgh”覆蓋。

你也可以更新字典1:

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

新版本的Python引入了兩個(gè)新的字典運(yùn)算符:合并(|)和更新(|=)。你可以使用|合并兩個(gè)字典,而|=用于更新字典:

  1. >>> pycon = {2016: "Portland", 2018: "Cleveland"} 
  2. >>> europython = {2017: "Rimini", 2018: "Edinburgh", 2019: "Basel"} 
  3.  
  4. >>> pycon | europython  # 合并 
  5. {2016: 'Portland', 2018: 'Edinburgh', 2017: 'Rimini', 2019: 'Basel'} 
  6.  
  7. >>> pycon |= europython # 更新 
  8. >>> pycon 
  9. {2016: 'Portland', 2018: 'Edinburgh', 2017: 'Rimini', 2019: 'Basel'} 

d1|d2和{** d1,** d2}的作用類似,都用于合并字典取并集,遇到相同key,后者會將前者覆蓋。

使用|的優(yōu)勢之一是它適用于類似字典的類型,并在合并后保持原來的類型:

  1. >>> from collections import defaultdict 
  2. >>> europe = defaultdict(lambda: "", {"Norway": "Oslo", "Spain": "Madrid"}) 
  3. >>> africa = defaultdict(lambda: "", {"Egypt": "Cairo", "Zimbabwe": "Harare"}) 
  4.  
  5. >>> europe | africa 
  6. defaultdict(<function <lambda> at 0x7f0cb42a6700>
  7.   {'Norway': 'Oslo', 'Spain': 'Madrid', 'Egypt': 'Cairo', 'Zimbabwe': 'Harare'}) 
  8.  
  9. >>> {**europe, **africa} 
  10. {'Norway': 'Oslo', 'Spain': 'Madrid', 'Egypt': 'Cairo', 'Zimbabwe': 'Harare'} 

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

  1. >>> libraries = { 
  2. ...     "collections": "Container datatypes", 
  3. ...     "math": "Mathematical functions", 
  4. ... } 
  5. >>> libraries |= {"zoneinfo": "IANA time zone support"} 
  6. >>> libraries 
  7. {'collections': 'Container datatypes', 'math': 'Mathematical functions', 
  8.  'zoneinfo': 'IANA time zone support'} 

|=還可以將類似字典的數(shù)據(jù)結(jié)構(gòu)用于更新:

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

2. 刪除字符串前綴和后綴

在Python 3.9中,可以使用.removeprefix()和.removesuffix()分別刪除字符串的開頭或結(jié)尾:

  1. >>> "three cool features in Python".removesuffix(" Python") 
  2. 'three cool features in' 
  3.  
  4. >>> "three cool features in Python".removeprefix("three ") 
  5. 'cool features in Python' 
  6.  
  7. >>> "three cool features in Python".removeprefix("Something else") 
  8. 'three cool features in Python' 

有人會說.strip方法也可以呀,但是該方法會出現(xiàn)誤刪操作:

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

可以看到,明明想刪掉結(jié)尾的單詞python,但是開頭的there也被刪除了一部分-Th。

所以.removeprefix()和.removesuffix()可能更精準(zhǔn)一些。

3. zoneinfo時(shí)區(qū)模塊

zoneinfo是python3.9新引入的模塊,zoneinfo可以訪問Internet號碼分配機(jī)構(gòu)(IANA)時(shí)區(qū)數(shù)據(jù)庫。IANA每年都會多次更新其數(shù)據(jù)庫,這是時(shí)區(qū)信息的最權(quán)威來源。

使用zoneinfo,可以獲得數(shù)據(jù)庫中描述任何時(shí)區(qū)的對象:

  1. >>> from zoneinfo import ZoneInfo 
  2. >>> ZoneInfo("America/Vancouver") 
  3. zoneinfo.ZoneInfo(key='America/Vancouver'
  1. >>> from zoneinfo import ZoneInfo 
  2. >>> from datetime import datetime, timedelta 
  3.  
  4. >>> # 夏令時(shí) 
  5. >>> dt = datetime(2020, 10, 31, 12, tzinfo=ZoneInfo("America/Los_Angeles")) 
  6. >>> print(dt) 
  7. 2020-10-31 12:00:00-07:00 
  8. >>> dt.tzname() 
  9. 'PDT' 
  10.  
  11. >>> # 標(biāo)準(zhǔn)時(shí)間 
  12. >>> dt += timedelta(days=7
  13. >>> print(dt) 
  14. 2020-11-07 12:00:00-08:00 
  15. >>> print(dt.tzname()) 
  16. PST 

4. 內(nèi)置集合類型用于類型提示

在類型提示中,現(xiàn)在可以將內(nèi)置集合類型(例如list和dict)用作泛型類型,而不必從typing中導(dǎo)入相應(yīng)的大寫類型(例如List或Dict)。

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

5. 拓?fù)渑判?/strong>

Python 3.9添加了一個(gè)新的模塊graphlib,其中包含graphlib.TopologicalSorter類,以提供執(zhí)行拓?fù)渑判虻墓δ堋?/p>

  1. >>> dependencies = { 
  2. ...     "realpython-reader": {"feedparser", "html2text"}, 
  3. ...     "feedparser": {"sgmllib3k"}, 
  4. ... } 
  5. ... 
  6.  
  7. >>> from graphlib import TopologicalSorter 
  8. >>> ts = TopologicalSorter(dependencies) 
  9. >>> list(ts.static_order()) 
  10. ['html2text', 'sgmllib3k', 'feedparser', 'realpython-reader'] 

6. 最小公倍數(shù)(LCM)

Python長期以來一直具有用于計(jì)算兩個(gè)數(shù)字的最大公約數(shù)(GCD)的功能:

  1. >>> import math 
  2. >>> math.gcd(49, 14) 

最小公倍數(shù)(LCM)與最大公約數(shù)(GCD)有關(guān),可以根據(jù)GCD定義LCM:

  1. >>> def lcm(num1, num2): 
  2. ...     if num1 == num2 == 0: 
  3. ...         return 0 
  4. ...     return num1 * num2 // math.gcd(num1, num2) 
  5. ... 
  6. >>> lcm(49, 14) 
  7. 98 

在Python 3.9中,不再需要定義自己的LCM函數(shù),它新增了計(jì)算最小公倍數(shù)功能:

  1. >>> import math 
  2. >>> math.lcm(49, 14) 
  3. 98 

7. 更強(qiáng)大的Python解析器

Python 3.9最酷的功能之一是大家在日常編程中不會注意到的功能,那就是解析器的更新。解析器是Python解釋器的基本組件。在最新版本中,解析器已重新構(gòu)建。

Python之前一直使用LL(1)解析器將源代碼解析為解析樹。你可以將LL(1)解析器視為一次讀取一個(gè)字符,并解釋源代碼而無需回溯的解析器。

新解釋器是基于PEG(parsing expression grammar)實(shí)現(xiàn)的,并非LL(1)。新解析器的性能可以與舊解析器媲美,在設(shè)計(jì)新語言功能時(shí),PEG比LL(1)更靈活。

在整個(gè)標(biāo)準(zhǔn)庫中,PEG解析器稍快一些,然而也使用了更多的內(nèi)存。實(shí)際上,使用新解析器時(shí),很難能感知到性能的好壞。

 

責(zé)任編輯:趙寧寧 來源: Python大數(shù)據(jù)分析
相關(guān)推薦

2020-10-14 15:00:38

Python 開發(fā)編程語言

2020-04-27 15:43:45

Python 3.9python開發(fā)

2021-01-04 08:15:16

CentOS 7Python3.9Python

2021-06-14 09:25:20

PythonPython 3.9編程語言

2021-01-20 23:53:16

PythonPython 3.9開發(fā)

2013-03-05 09:35:54

Linux

2019-11-25 15:58:15

Python 3.9新特性模塊

2012-05-22 01:31:30

Highlight代碼工具Java

2020-10-31 21:50:54

Python3.9Python開發(fā)

2020-09-28 07:56:16

Python3.9Python開發(fā)

2021-05-26 16:10:00

Python 開發(fā)編程語言

2014-04-17 11:45:39

Ultimate EdLinux 發(fā)行版

2021-04-16 16:21:02

鴻蒙HarmonyOS應(yīng)用開發(fā)

2025-01-15 10:02:09

APIVueDOM

2020-07-10 06:40:31

Python 3.9Python開發(fā)

2024-05-08 08:50:39

React19模式UI

2010-02-03 13:25:34

云計(jì)算

2022-11-29 07:48:16

2020-10-09 10:29:05

Python 開發(fā)編程語言

2020-07-15 15:26:20

書籍Python學(xué)習(xí)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號