Python 3.9,来了!

Python 3.9,来了!

10多年的大石桥网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网整合营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整大石桥建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“大石桥网站设计”,“大石桥网站推广”以来,每个客户项目都认真落实执行。

过去一年,来自世界各地的开发者们一直在致力于Python3.8的改进。Python 3.9 beta版本已经存在了一段时间,第一个正式版本于2020年10月5日发布。

每个Python版本都包含新开发和改进的功能,Python 3.9也不例外。

下面介绍Python 3.9几个主要的新功能。

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

字典是Python中最基础的数据结构之一,并且随着python版本的迭代,性能得到不断地优化。

Python3.9中,合并(|)和更新(|=)运算符已添加到dict类中。这些更新完善了现有的dict.update和{** d1,** d2}方法。

传统合并字典的方法:

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

这两种方法都合并了字典而不更改原始数据。请注意,字典1中“Cleveland”已被合并的字典2中“Edinburgh”覆盖。

你也可以更新字典1:

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

新版本的Python引入了两个新的字典运算符:合并(|)和更新(|=)。你可以使用|合并两个字典,而|=用于更新字典:

 
 
 
 
  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,后者会将前者覆盖。

使用|的优势之一是它适用于类似字典的类型,并在合并后保持原来的类型:

 
 
 
 
  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( 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'} 

|=还可以将类似字典的数据结构用于更新:

 
 
 
 
  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()分别删除字符串的开头或结尾:

 
 
 
 
  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方法也可以呀,但是该方法会出现误删操作:

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

可以看到,明明想删掉结尾的单词python,但是开头的there也被删除了一部分-Th。

所以.removeprefix()和.removesuffix()可能更精准一些。

3. zoneinfo时区模块

zoneinfo是python3.9新引入的模块,zoneinfo可以访问Internet号码分配机构(IANA)时区数据库。IANA每年都会多次更新其数据库,这是时区信息的最权威来源。

使用zoneinfo,可以获得数据库中描述任何时区的对象:

 
 
 
 
  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. >>> # 夏令时 
  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. >>> # 标准时间 
  12. >>> dt += timedelta(days=7) 
  13. >>> print(dt) 
  14. 2020-11-07 12:00:00-08:00 
  15. >>> print(dt.tzname()) 
  16. PST 

4. 内置集合类型用于类型提示

在类型提示中,现在可以将内置集合类型(例如list和dict)用作泛型类型,而不必从typing中导入相应的大写类型(例如List或Dict)。

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

5. 拓扑排序

Python 3.9添加了一个新的模块graphlib,其中包含graphlib.TopologicalSorter类,以提供执行拓扑排序的功能。

 
 
 
 
  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. 最小公倍数(LCM)

Python长期以来一直具有用于计算两个数字的最大公约数(GCD)的功能:

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

最小公倍数(LCM)与最大公约数(GCD)有关,可以根据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函数,它新增了计算最小公倍数功能:

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

7. 更强大的Python解析器

Python 3.9最酷的功能之一是大家在日常编程中不会注意到的功能,那就是解析器的更新。解析器是Python解释器的基本组件。在最新版本中,解析器已重新构建。

Python之前一直使用LL(1)解析器将源代码解析为解析树。你可以将LL(1)解析器视为一次读取一个字符,并解释源代码而无需回溯的解析器。

新解释器是基于PEG(parsing expression grammar)实现的,并非LL(1)。新解析器的性能可以与旧解析器媲美,在设计新语言功能时,PEG比LL(1)更灵活。

在整个标准库中,PEG解析器稍快一些,然而也使用了更多的内存。实际上,使用新解析器时,很难能感知到性能的好坏。

新闻标题:Python 3.9,来了!
网页路径:http://www.hantingmc.com/qtweb/news20/519170.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联