糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > 来喽 来喽 Python 3.9正式版发布了~~~

来喽 来喽 Python 3.9正式版发布了~~~

时间:2022-03-29 02:42:12

相关推荐

来喽 来喽 Python 3.9正式版发布了~~~

摘要:10月5日,在全国人员欢度国庆节和中秋节时,Python 3.9 悄摸摸地正式发布了。

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,提醒大家升级时需要注意什么,条理很清晰。

安装

到10月9日为止,anaconda上还没有任何channel支持对python 3.9的直接安装,所以想尝鲜,有2种方法:

1.到上下载安装包

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 -VPython 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': <class 'str'>}

这个时候就会比较有用,我们可以这样写:

names: list[str]

而不用像之前那样:

names: List[str]

新的解析器

Python 3.9 开始使用新的解析器,基于 PEG,而取代LL(1)。两者的性能相差不大,但PEG更灵活。从这里我们可以推断,从Python 3.10开始,将会引入更多新的语言特性。

zoneinfo

这个新模块,在我们操作时区时,会比较方便。之前我们处理timezone时,需要通过pytz包,比如:

# py38import pytzfrom datetime import datetimetz = pytz.timezone("America/Los_Angeles")start_time = datetime.now(tz)

现在可以通过标准库中的zoneinfo模块,比如:

from zoneinfo import ZoneInfotz = ZoneInfo("America/Los_Angeles")

其它变化

在python 3.8中,Vectorcall协议被临时引入,3.9中,对内置类型,包括,range, tuple, set, frozenset, list, dict,都使用vectorcall协议进行了优化。但有趣的是,从性能优化报告中,我们可以看到,从3.8到3.9的性能并没有什么提升,甚至有小幅下降。

Python version 3.43.53.63.73.8 3.9-------------- --------------- ---Variable and attribute read access:read_local 7.17.15.45.13.9 4.0read_nonlocal7.18.15.85.44.4 4.8read_global 15.5 19.0 14.3 13.67.6 7.7read_builtin21.1 21.6 18.5 19.07.5 7.7read_classvar_from_class 25.6 26.5 20.7 19.5 18.4 18.6read_classvar_from_instance22.8 23.5 18.8 17.1 16.4 20.1read_instancevar32.4 33.1 28.0 26.3 25.4 27.7read_instancevar_slots27.8 31.3 20.8 20.8 20.2 24.5read_namedtuple 73.8 57.5 45.0 46.8 18.4 23.2read_boundmethod37.6 37.9 29.6 26.9 27.7 45.9Variable and attribute write access:write_local 8.79.35.55.34.3 4.2write_nonlocal 10.5 11.15.65.54.7 4.9write_global19.7 21.2 18.0 18.0 15.8 17.2write_classvar 92.9 96.0 104.6 102.1 39.2 43.2write_instancevar44.6 45.8 40.0 38.9 35.5 40.7write_instancevar_slots 35.6 36.1 27.3 26.6 25.7 27.7Data structure read access:read_list 24.2 24.5 20.8 20.8 19.0 21.1read_deque 24.7 25.5 20.2 20.6 19.8 21.6read_dict 24.3 25.7 22.3 23.0 21.0 22.5read_strdict22.6 24.3 19.5 21.2 18.9 21.6Data structure write access:write_list 27.1 28.5 22.5 21.6 20.0 21.6write_deque 28.7 30.1 22.7 21.8 23.5 23.2write_dict 31.4 33.3 29.3 29.2 24.7 27.8write_strdict 28.4 29.9 27.5 25.2 23.1 29.8Stack (or queue) operations:list_append_pop 93.4 112.7 75.4 74.2 50.8 53.9deque_append_pop43.5 57.0 49.4 49.2 42.5 45.5deque_append_popleft 43.7 57.3 49.7 49.7 42.8 45.5Timing loop:loop_overhead0.50.60.40.30.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

What’s new in python 3.9,/3/whatsnew/3.9.htmlWhat’s new in python 3.8,/3/whatsnew/3.8.htmlConda-forge python files,/conda-forge/python/filesPython 3.9 tar,/conda-forge/python/3.9.0/download/win-64/python-3.9.0-h60c2a47_1_cpython.tar.bz2

点击关注,第一时间了解华为云新鲜技术~

如果觉得《来喽 来喽 Python 3.9正式版发布了~~~》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。