日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python3.0正式发布的年份是_来喽,来喽,Python 3.9正式版发布了~~~

發布時間:2023/12/14 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3.0正式发布的年份是_来喽,来喽,Python 3.9正式版发布了~~~ 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

摘要:2020年10月5日,在全國人員歡度國慶節和中秋節時,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種方法:

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

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

$ conda create -n py39 -c conda-forge -y

$ conda activate py39

$ conda install python-3.9.0-h60c2a47_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 Features

Dictionary 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': }

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

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

References

總結

以上是生活随笔為你收集整理的python3.0正式发布的年份是_来喽,来喽,Python 3.9正式版发布了~~~的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。