Py学习  »  Python

Python 3.9 值得关注的更新点

生信菜鸟团 • 3 年前 • 455 次点击  

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魔法方法是什么

使用Python实现基本初等函数可视化

如何将pdf转换为word 3.0

使用 Python 操作 word文档

如何在 Rstudio 中使用 python 语言 (图文详解)

适用于任何学科| 10个好用的 Python数据可视化库

好看又好用的python可视化包

4种绘制带误差线的柱形图



参考资料

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


文末友情推荐

要想真正入门生物信息学建议务必购买全套书籍,一点一滴攻克计算机基础知识,书单在:什么,生信入门全套书籍仅需160 。如果大家没有时间自行慢慢摸索着学习,可以考虑我们生信技能树官方举办的学习班:

数据挖掘学习班第5期(线上直播3周,马拉松式陪伴,带你入门),原价4800的数据挖掘全套课程, 疫情期间半价即可抢购。生信爆款入门-第7期(线上直播4周,马拉松式陪伴,带你入门),原价9600的生信入门全套课程,疫情期间3.3折 即可抢购。

如果你课题涉及到转录组,欢迎添加一对一客服:详见:你还在花三五万做一个单细胞转录组吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/72690
 
455 次点击