知行编程网知行编程网  2022-07-04 16:00 知行编程网 隐藏边栏 |   抢沙发  37 
文章评分 0 次,平均分 0.0

Python 3.9 的这十个新特性太棒了吧!

选自 | towardsdatascience  作者 | Farhad Malik

编译 | 机器之心   编辑 | 陈萍
近日,Python 3.9 发布,并开发了一些新特性,包括字典合并与更新、新的解析器、新的字符串函数等。
Python 3.9 已于 10 月 5 日发布,新版本的特性,你 get 到了吗?对于 Python 程序员来说,这又是一个令人兴奋的时刻。

Python 3.9 的这十个新特性太棒了吧!


相比于之前的版本,Python 3.9 有哪些值得讨论的功能呢?

简而言之,从字典更新 / 合并到添加新的字符串方法,再到 zoneinfo 库的引入,Python 3.9 添加了许多新特性。此外,该版本还引入了一种稳定、高性能的新型解析器。

标准库更新了许多新特性,并添加了新模块 zoneinfo 和 graphlib。此外该版本也对许多模块进行了改进,如 ast、asyncio、concurrent.futures、multiprocessing、xml 等。

Python 3.9 的这十个新特性太棒了吧!
图源:https://twitter.com/PrasoonPratham/status/1313392420038483968

现在让我们一起探索 Python 3.9 的新特性。

   Python 3.9:10 个有趣的新特性

1. 字典更新和合并

字典添加两个新的运算符:「|」和「|=」。「|」运算符用于合并字典,「|=」运算符用于更新字典。

字典合并:


字典更新:


2. 基于 PEG 的高性能解析器

Python 3.9 提出用高性能和稳定的基于 PEG 的解析器取代当前基于 LL(1) 的 Python 解析器。

当前的 CPython 解析器基于 LL(1),LL(1) 解析器是一个自顶向下的解析器,它从左到右解析输入。

Python 3.9 提议将 LL(1) 替换为新的基于 PEG 的解析器,这意味着它将解除当前 LL(1) 语法对 Python 的限制。此外,当前的解析器修补了许多将要删除的 hack。因此,从长远来看,这会降低维护成本。

3. 新型字符串函数:删除前缀和后缀

Python 3.9 将两个新函数添加到 str 对象:

第一个函数用于删除前缀:str.removeprefix(prefix)
第二个函数用于删除后缀:str.removesuffix(suffix)


4. 对内置泛型类型提供类型提示

Python 3.9 通过删除并行类型层次结构,使注释程序变得更加简单。Python 3.9 支持 typing 模块所有标准集合中的泛型语法。

我们可以将 list 或 dict 直接作为列表和字典的类型注释,而不必依赖 typing.List 或者 typing.Dict。因此,代码现在看起来更加简洁,而且更容易理解和解释。


5. DateTime 支持 IANA 时区

zoneinfo 模块被创建出来支持 IANA 时区数据库。对 IANA 时区数据库的支持已经添加到标准库中。

IANA 时区通常称为 tz 或 zone info。存在许多具备不同搜索路径的 IANA 时区,用于为 date-time 对象指定 IANA 时区。例如,我们可以将对 datetime 对象设置搜索路径「Continent/City」来设置 tzinfo。


如果传入一个无效的密钥,则会引发 zoneinfo.ZoneInfoNotFoundError 异常。

6. concurrent.futures 优化

concurrent.futures.Executor.shutdown() 中添加了一个新参数 cancel_futures。此参数可以取消尚未执行的并发任务。在 Python 3.9 之前主进程只有在所有并发任务完成后才能关闭 executor 对象。

新参数 cancel_futures 已被添加到 ThreadPoolExecutor 以及 ProcessPoolExecutor。它的工作方式是:当参数的值为 True 时,在调用 shutdown() 函数时取消所有待处理的任务。

7. 异步编程和多进程优化

Python 3.9 对异步编程(asyncio)和多进程库进行了优化。

1. 由于安全问题的考虑,asyncio.loop.create_datagram_endpoint() 不再支持参数 reuse_address。

2. 新增了 coroutines、shutdown_default_executor() 和 asyncio.to_thread() 。shutdown_default_executor 负责关闭默认 executor,asyncio.to_thread() 主要用于在一条单独的线程中运行 IO 密集型函数,以避免事件循环。

关于多进程库的改进,Python 3.9 向 multiprocessing.SimpleQueue 类添加了新方法 close()。

此方法可以显式地关闭队列。这将确保队列关闭并且停留时间不会比预期长。值得注意的是,一旦关闭队列,就不能调用 get()、put() 和 empty() 方法。

8. 统一的软件包导入错误

Python3.9 之前的版本在导入 Python 库时存在的主要问题是:当相对导入超出其顶级包时,Python 中的导入行为不一致。

builtins.__import__() 引发 ValueError,而 importlib.__import__() 引发 ImportError。Python3.9 版本进行了修复,它将统一引发 ImportError。

9. 生成随机字节

Python 3.9 版本中添加了新的函数 random.Random.randbytes()。该函数可用于生成随机字节。

Python 支持生成随机数,但如果需要生成随机字节呢?在 Python3.9 版本之前,开发人员必须想方设法生成随机字节。尽管他们可以使用 os.getrandom()、os.urandom() 或者 secrets.token_bytes() 来生成随机字节,但这些方法无法生成伪随机模式。

为了确保随机数的产生符合预期行为,并且过程可复现,开发人员通常将种子(seed)与 random.Random 模块一起使用。因此,Python 3.9 添加了 random.Random.randbytes() 方法,以可控的方式生成随机字节。

10. 修复字符串替换函数

在 Python 3.9 版本之前,对于所有非零的 n,"".replace("",s,n) 返回空字符串而不是 s。这个错误使用户困惑,并导致应用程序的不一致行为。

Python 3.9 修复了该问题,不管 n 是否为 0,其结果都与 "".replace("", s) 一致。


如果大家想深入了解更多细节,请查阅官方文档:https://docs.python.org/zh-cn/3.9/whatsnew/3.9.html

原文链接:https://towardsdatascience.com/10-awesome-python-3-9-features-b8c27f5eba5c

<section data-brushtype="text" style="padding-right: 0em;padding-left: 0em;white-space: normal;max-width: 100%;letter-spacing: 0.544px;color: rgb(62, 62, 62);font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;widows: 1;word-spacing: 2px;caret-color: rgb(255, 0, 0);background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;color: rgb(0, 0, 0);font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;box-sizing: border-box !important;overflow-wrap: break-word !important;">—</span></strong>完<strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;box-sizing: border-box !important;overflow-wrap: break-word !important;">—</span></strong></span></strong></span></strong></section><pre style="padding-right: 0em;padding-left: 0em;max-width: 100%;letter-spacing: 0.544px;color: rgb(62, 62, 62);font-size: 16px;widows: 1;word-spacing: 2px;caret-color: rgb(255, 0, 0);background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><pre style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;letter-spacing: 0.544px;white-space: normal;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-top: 15px;margin-bottom: 25px;max-width: 100%;opacity: 0.8;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-top: 15px;margin-bottom: 25px;max-width: 100%;opacity: 0.8;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-bottom: 15px;padding-right: 0em;padding-left: 0em;max-width: 100%;color: rgb(127, 127, 127);font-size: 12px;font-family: sans-serif;line-height: 25.5938px;letter-spacing: 3px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 16px;font-family: 微软雅黑;caret-color: red;box-sizing: border-box !important;overflow-wrap: break-word !important;">为您推荐</span></strong></span></section><section style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;box-sizing: border-box !important;overflow-wrap: break-word !important;">干货 | 算法工程师超实用技术路线图</section><section style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 14px;">Github上10个超好看的可视化面板,nice!</span></section><section style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 14px;">吴恩达上新:生成对抗网络(GAN)专项课程</span><br  /></section><section style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;box-sizing: border-box !important;overflow-wrap: break-word !important;">拿到2021年灰飞烟灭的算法岗offer的大佬们是啥样的?<br  /></section><section style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;">你一定从未看过如此通俗易懂的YOLO系列解读 (下)</section></section></section></section></section></section></section></section></section>

Python 3.9 的这十个新特性太棒了吧!

本篇文章来源于: 深度学习这件小事

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写

发表评论

表情 格式 链接 私密 签到
扫一扫二维码分享