久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

Python 3.9有哪些特點

145次閱讀
沒有評論

共計 5360 個字符,預計需要花費 14 分鐘才能閱讀完成。

本篇內容主要講解“Python 3.9 有哪些特點”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓丸趣 TV 小編來帶大家學習“Python 3.9 有哪些特點”吧!

2020 年 10 月 5 日,在全國人員歡度國慶節和中秋節時,Python 3.9 悄摸摸地正式發布了。我們來一起來看看,這個版本有哪些好玩的新特性,以及對我們部門目前的產品可能會帶來哪些影響。

因為 jupyter notebook/lab 等工具還沒有相應適配到 python 3.9,所以我們還無法使用,因此本文就使用 python 的交互行來演示。

Python 3.9 官方文檔,What’s New in Python 3.9,其文字組織的很好,我們接下來也按照這個順序來講解,依次是,release highlights, new features, new modules, improve modules, optimizations, deprecated, removed. 大家注意看下,這個文字組織順序,其實在我們產品發布時,也是適用的。先講這個版本有什么吸引人的 highlights,然后介紹新版本的新內容,最后介紹 deprecated / removed,提醒大家升級時需要注意什么,條理很清晰。

安裝

到 2020 年 10 月 9 日為止,anaconda 上還沒有任何 channel 支持對 python 3.9 的直接安裝,所以想嘗鮮,有 2 種方法:

1. 到 http://python.org 上下載安裝包

2. 到 anaconda 的 conda-forge channel 下載安裝文件

我們使用第二種方法,安裝文件下載鏈接見 References。

$ conda create -n py39 -c conda-forge -y
 $ conda activate py39
 $ conda install python-3.9.0-h70c2a47_1_cpython.tar.bz2
  
 $ which python
 /d/Anaconda3/envs/py39/python
 $ python -V
 Python 3.9.0

Release Highlights

Python 3.9 內容包括:

3 個新的語法特性

1 個新的內置特性

2 個新的標準庫特性

6 點解釋器提升

2 個新的庫模塊

如果把以上所有內容都過一遍,可能需要 1 - 2 小時。我們這里就挑一些與我們部門產品開發相關的內容,具體來講一講,其它內容如果有興趣,可以自行去讀讀。

New FeaturesDictionary Merge Update Operators

dict 類提供了 merge (|) 和 update (|=) 操作符。

# py38
  x = {key1 :  value1 from x ,  key2 :  value2 from x}
  y = {key2 :  value2 from y ,  key3 :  value3 from y}
  {**x, **y}
 {key1 :  value1 from x ,  key2 :  value2 from y ,  key3 :  value3 from y}
  x.update(y)
  x
 {key1 :  value1 from x ,  key2 :  value2 from y ,  key3 :  value3 from y}
  
 # py39
  x = {key1 :  value1 from x ,  key2 :  value2 from x}
  y = {key2 :  value2 from y ,  key3 :  value3 from y}
  x | y
 {key1 :  value1 from x ,  key2 :  value2 from y ,  key3 :  value3 from y}
  y | x
 {key2 :  value2 from x ,  key3 :  value3 from y ,  key1 :  value1 from x}

這在 dict 操作時,會更方便。

New string methods to remove prefixes and suffixes

  NavyXie .removeprefix(Navy)
 Xie 
   NavyXie .removesuffix(Xie)
 Navy

這在 string 刪除不需要的 prefix 或 suffix 時,會更方便。

Type hinting generics in standard collections

在 type annotation 中,可以使用內置的 collection 類型,如 list 和 dict,而不用導入相應的大寫類型,如 typing.List 或 typing.Dict。

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

Annotation 是 python 3.0 引入的特征,是做什么用的呢?與 Java / C / C++ / Swift 等強類型語言不同,Python 和 JavaScript 都是弱類型語言,這里類型 annotation 并不會在解析或運行時強制要求傳參的類型,而只是幫助開發者的代碼閱讀和維護。

另外,如果我們使用 python 3.7 引入的庫,dataclasses,時,就會發現,type annotation 在定義一個 data 類時,是強制要求的,比如:

 from dataclasses import dataclass
  @dataclass
 ... def TestClass:
 ... name: str
  TestClass.__annotations__
 {name :  class  str}

這個時候就會比較有用,我們可以這樣寫:

names: list[str]

而不用像之前那樣:

names: List[str]

新的解析器

Python 3.9 開始使用新的解析器,基于 PEG,而取代 LL(1)。兩者的性能相差不大,但 PEG 更靈活。從這里我們可以推斷,從 Python 3.10 開始,將會引入更多新的語言特性。

zoneinfo

這個新模塊,在我們操作時區時,會比較方便。之前我們處理 timezone 時,需要通過 pytz 包,比如:

# py38
 import pytz
 from datetime import datetime
  
 tz = pytz.timezone(America/Los_Angeles)
 start_time = datetime.now(tz)

現在可以通過標準庫中的 zoneinfo 模塊,比如:

from zoneinfo import ZoneInfo
  
 tz = ZoneInfo(America/Los_Angeles)

其它變化

在 python 3.8 中,Vectorcall 協議被臨時引入,3.9 中,對內置類型,包括,range, tuple, set, frozenset, list, dict,都使用 vectorcall 協議進行了優化。但有趣的是,從性能優化報告中,我們可以看到,從 3.8 到 3.9 的性能并沒有什么提升,甚至有小幅下降。

Python version 3.4 3.5 3.6 3.7 3.8 3.9
 -------------- --- --- --- --- --- ---
  
 Variable and attribute read access:
  read_local 7.1 7.1 5.4 5.1 3.9 4.0
  read_nonlocal 7.1 8.1 5.8 5.4 4.4 4.8
  read_global 15.5 19.0 14.3 13.6 7.6 7.7
  read_builtin 21.1 21.6 18.5 19.0 7.5 7.7
  read_classvar_from_class 25.6 26.5 20.7 19.5 18.4 18.6
  read_classvar_from_instance 22.8 23.5 18.8 17.1 16.4 20.1
  read_instancevar 32.4 33.1 28.0 26.3 25.4 27.7
  read_instancevar_slots 27.8 31.3 20.8 20.8 20.2 24.5
  read_namedtuple 73.8 57.5 45.0 46.8 18.4 23.2
  read_boundmethod 37.6 37.9 29.6 26.9 27.7 45.9
  
 Variable and attribute write access:
  write_local 8.7 9.3 5.5 5.3 4.3 4.2
  write_nonlocal 10.5 11.1 5.6 5.5 4.7 4.9
  write_global 19.7 21.2 18.0 18.0 15.8 17.2
  write_classvar 92.9 96.0 104.6 102.1 39.2 43.2
  write_instancevar 44.6 45.8 40.0 38.9 35.5 40.7
  write_instancevar_slots 35.6 36.1 27.3 26.6 25.7 27.7
  
 Data structure read access:
  read_list 24.2 24.5 20.8 20.8 19.0 21.1
  read_deque 24.7 25.5 20.2 20.6 19.8 21.6
  read_dict 24.3 25.7 22.3 23.0 21.0 22.5
  read_strdict 22.6 24.3 19.5 21.2 18.9 21.6
  
 Data structure write access:
  write_list 27.1 28.5 22.5 21.6 20.0 21.6
  write_deque 28.7 30.1 22.7 21.8 23.5 23.2
  write_dict 31.4 33.3 29.3 29.2 24.7 27.8
  write_strdict 28.4 29.9 27.5 25.2 23.1 29.8
  
 Stack (or queue) operations:
  list_append_pop 93.4 112.7 75.4 74.2 50.8 53.9
  deque_append_pop 43.5 57.0 49.4 49.2 42.5 45.5
  deque_append_popleft 43.7 57.3 49.7 49.7 42.8 45.5
  
 Timing loop:
  loop_overhead 0.5 0.6 0.4 0.3 0.3 0.3

備注:以上結果是 python 官方 benchmark, Tools/scripts/var_access_benchmark.py, 的運行結果,單位為納秒,硬件為 Intel? Core? i7-4960HQ 處理器,OS 為 macOS 64-bit。

注意 Deprecated / Removed

我提取了一些與我們部門產品可能相關度比較高的幾點:

(1)Python 3.9 是提供 Python 2 向后兼容的最后一個版本,所以在下個版本 Python 3.10 將不在兼容 Python 2。

(2)threading.Thread 類的 isAlive() 方法被刪除,用 is_alive() 取代。

(3)base64.encodestring() 和 base64.decodestring() 被刪除,用 base64.encodebytes() 和 base64.decodebytes() 取代。

(4)json.loads() 的 encoding 參數被刪除,encoding 必須為 UTF-8, UTF-16 或 UTF-32.

復習 Python 3.8 的幾點特性

最后,我們再復習下 python 3.8 的幾點新特性,如果工作中沒有嘗試過,那就馬上試試吧。

海象操作符 :=

if (n := len(a))   10:
  print(f List is too long ({n} elements, expected  = 10) )

Positional-only 參數

def f(a, b, /, c, d, *, e, f):
  print(a, b, c, d, e, f)

f-string 支持 =

 user =  eric_idle 
  member_since = date(1975, 7, 31)
  f {user=} {member_since=} 
 user= eric_idle  member_since=datetime.date(1975, 7, 31) 
  
  delta = date.today() - member_since
  f {user=!s} {delta.days=:,d} 
 user=eric_idle delta.days=16,075 
  
  print(f {theta=} {cos(radians(theta))=:.3f} )
 theta=30 cos(radians(theta))=0.866

到此,相信大家對“Python 3.9 有哪些特點”有了更深的了解,不妨來實際操作一番吧!這里是丸趣 TV 網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-08-25發表,共計5360字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 桓仁| 且末县| 通许县| 武城县| 闽侯县| 广东省| 鄂托克前旗| 通榆县| 永州市| 北流市| 龙里县| 文昌市| 涡阳县| 张家川| 达拉特旗| 苏尼特左旗| 长岭县| 密云县| 隆回县| 敖汉旗| 饶平县| 峡江县| 方正县| 内乡县| 清河县| 湖北省| 绥德县| 沁阳市| 东莞市| 宁强县| 沁源县| 金山区| 阳西县| 木里| 明水县| 含山县| 龙口市| 明光市| 辽中县| 阿图什市| 彭水|