前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 3.9 值得关注的更新点

Python 3.9 值得关注的更新点

作者头像
生信菜鸟团
发布2020-08-28 09:52:51
4690
发布2020-08-28 09:52:51
举报
文章被收录于专栏:生信菜鸟团生信菜鸟团

Python更新不停。

2020年8月19日,Python 最新释放bate版本 3.9.0rc1,呼之欲出的新版,提前来看看吧~

新功能

  • 新增字典合并方法
  • 新增字符串删除操作内置函数
  • 类型提示与定义
  • 时区对象设置
  • 新的 Python 解析器

新增字典合并方法

再一次优雅。在3.8版本之前,字典合并需要使用 zip() 或其他的方法进行,而现在只需要使用 |即可快速完成预期,需要注意的是,当两个字典有相同的键时,对应的值为最后一次的赋值:

>>> d = {'spam': 1, 'eggs': 2, 'cheese': 3}
>>> e = {'cheese': 'cheddar', 'aardvark': 'Ethel'}
>>> d | e
{'spam': 1, 'eggs': 2, 'cheese': 'cheddar', 'aardvark': 'Ethel'}
>>> e | d
{'aardvark': 'Ethel', 'spam': 1, 'eggs': 2, 'cheese': 3}

若要直接更新字典,那么使用 |= 即可

>>> d |= e
>>> d
{'spam': 1, 'eggs': 2, 'cheese': 'cheddar', 'aardvark': 'Ethel'}

逻辑有点类似上一期魔法方法中我们说道的 +=,即a+=b等价于 a =a+b

新增字符串操作内置函数

removeprefix()removesuffix():虽然更新的大,但是这个的优势在于:

  • 不依赖统计字符串长度
  • 不需要调用lenstr.replace()函数
  • 与传统的字符串切片方法相比,这些方法为代码的可读性提供了更高级别的API。

举几个案例:

# Current
if funcname.startswith("context."):
    self.funcname = funcname.replace("context.", "")
    self.contextfunc = True
else:
    self.funcname = funcname
    self.contextfunc = False

# Improved
if funcname.startswith("context."):
    self.funcname = funcname.removeprefix("context.")
    self.contextfunc = True
else:
    self.funcname = funcname
    self.contextfunc = False

又比如:

# Current
if name.endswith(('Mixin', 'Tests')):
    return name[:-5]
elif name.endswith('Test'):
    return name[:-4]
else:
    return name

# Improved
return (name.removesuffix('Mixin')
            .removesuffix('Tests')
            .removesuffix('Test'))

上述可以直接理解为,在正序和倒序字符串排列中,删除部分已知的字符串内容,而不需要使用符串切片。

类型提示

现在,在3.5的基础上,python的编辑器能够快速响应指定并且理解我们的意图。

上图我们将 sum_dict 函数的参数定义为字典类型,将其返回值定义为 int 类型。test 的定义时也指定了类型。

时区对象

zoneinfo 模块有助于从 IANA 时区数据库中获得对应的信息,用于优化填充时区对象,简单使用如下:

>>> print(datetime(2020, 2, 22, 12, 0).astimezone())
2020-02-22 12:00:00-05:00
>>> print(datetime(2020, 2, 22, 12, 0).astimezone()
...       .strftime("%Y-%m-%d %H:%M:%S %Z"))
2020-02-22 12:00:00 EST
>>> print(datetime(2020, 2, 22, 12, 0).astimezone(timezone.utc))
2020-02-22 17:00:00+00:00

新的解析器

Python 目前主要使用一种基于 LL (1)的语法,而这种语法可以通过 LL (1)解析器进行解析——该解析器从上到下、从左到右地解析代码,只需要从词法分析器中取出一个 token 就可以正确地解析下去。

这个本应该是最大的修改点,但由于不是特别了解底层的工作原理,这里就贴一下官方的overview,详情可以通过查看原文了解。

This PEP proposes replacing the current LL(1)-based parser of CPython with a new PEG-based parser. This new parser would allow the elimination of multiple "hacks" that exist in the current grammar to circumvent the LL(1)-limitation. It would substantially reduce the maintenance costs in some areas related to the compiling pipeline such as the grammar, the parser and the AST generation. The new PEG parser will also lift the LL(1) restriction on the current Python grammar.

在更新日志中还提及到一些语言特性修改、模块的删减、弃用和API的变化,有兴趣小伙伴可以的查看官方更新日志可见:

https://docs.python.org/3.9/whatsnew/changelog.html#changelog

Python3.9版本都要来啦,点击Python3专辑,你将获得更多资料,我们下期见。


参考资料

Python 3.9 beta2 版本发布了,看看这 7 个新的 PEP 都是什么?

Python 3.9 正式版要来了,我还在3.6的路上!

What’s New In Python 3.9

https://docs.python.org/3.9/whatsnew/3.9.html#what-s-new-in-python-3-9

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-08-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 生信菜鸟团 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 新功能
  • 新增字典合并方法
  • 新增字符串操作内置函数
  • 类型提示
  • 时区对象
  • 新的解析器
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档