<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Python 3.9,來了!

          共 5051字,需瀏覽 11分鐘

           ·

          2020-10-11 07:42

          閱讀本文大概需要 3 分鐘。


          Python 3.9,來了!

          過去一年,來自世界各地的開發(fā)者們一直在致力于 Python3.8 的改進(jìn)。Python 3.9 beta 版本已經(jīng)存在了一段時間,第一個正式版本于 2020年 10 月 5 日發(fā)布。

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

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

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

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

          Python3.9 中,合并 (|) 和更新 (|=) 運算符已添加到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'}

          這兩種方法都合并了字典而不更改原始數(shù)據(jù)。請注意,字典 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,后者會將前者覆蓋。

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

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

          >>>?europe?|?africa
          defaultdict(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'}

          |=還可以將類似字典的數(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'}

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

          在 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'

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

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

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

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

          3. zoneinfo時區(qū)模塊

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

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

          >>>?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'

          >>>?#?標(biāo)準(zhǔn)時間
          >>>?dt?+=?timedelta(days=7)
          >>>?print(dt)
          2020-11-07?12:00:00-08:00
          >>>?print(dt.tzname())
          PST

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

          在類型提示中,現(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)

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

          Python 3.9 添加了一個新的模塊 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']

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

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

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

          最小公倍數(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

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

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

          7. 更強大的 Python 解析器

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

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

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

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

          參考:realpython、python 文檔


          推薦閱讀

          1

          Python 為什么不支持 switch 語句?

          2

          Linux!為何他一人就寫出這么強的系統(tǒng),中國卻做不出來?

          3

          奇技淫巧:在 ssh 里面把服務(wù)器的文本復(fù)制到本地電腦

          4??

          超全!我把 Python 的 200個標(biāo)準(zhǔn)庫整理出來了




          崔慶才

          靜覓博客博主,《Python3網(wǎng)絡(luò)爬蟲開發(fā)實戰(zhàn)》作者

          隱形字

          個人公眾號:進(jìn)擊的Coder

          長按識別二維碼關(guān)注





          好文和朋友一起看~
          瀏覽 51
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日本在线A片 | 日韩在线视频一区 | 操逼的乐趣 | 青春草在线视频免费观看 | 欧美在线视频导航 |