Python3.11正式版,它来了!
转载请注明出处️
作者:测试蔡坨坨
原文链接:caituotuo.top/b055fbf2.html
你好,我是测试蔡坨坨。
就在前几天,2022年10月24日,Python3.11正式版发布了!

Python官方在2020年1月1日结束了对Python2的维护,这也意味着Python2已经成为历史,真正进入了Python3时代。自从进入Python3版本以来,官方已经发布了众多修改版本分支,现在最新的正式版本就是前不久刚发布的Python3.11,这一版本历经17个月的开发,现在公开可用,据说对用户更友好。
今天,我们就来一起看看Python3.11都更新了些什么呢。
官方文档:https://docs.python.org/3.11/whatsnew/3.11.html
# author: 测试蔡坨坨
# datetime: 2022/10/29 15:14
# function: Python3.11 输出版本信息
import sys
print("Python Version: ", sys.version)
# Python Version: 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]
print("Python Version Info: ", sys.version_info)
# Python Version Info: sys.version_info(major=3, minor=11, micro=0, releaselevel='final', serial=0)
速度更快
首先第一点,也是最重要的,就是它更快了,官方说法是Python 3.11比Python 3.10快10-60%。
Python 3.11 is between 10-60% faster than Python 3.10. On average, we measured a 1.25x speedup on the standard benchmark suite. See Faster CPython for details.
众所周知,Python语法简洁、功能强大,特别容易上手,因此使用人数超级多,在众多最受欢迎编程语言榜单中多次位列第一,但是它有一个明显的劣势就是纯Python编写的程序执行效率低,很多开发者都期待这门语言的性能有所提升。
更加人性化的报错
PEP 657: Fine-grained error locations in tracebacks
官方文档:https://docs.python.org/3.11/whatsnew/3.11.html#whatsnew311-pep657
When printing tracebacks, the interpreter will now point to the exact expression that caused the error, instead of just the line.
更加人性化的报错,与之前显示在某一行的报错不同,这个版本的报错会显示具体原因,画出具体位置。
Python编程语言对初学者非常友好,具有易于理解的语法和强大的数据结构,但是对于刚刚接触Python的同学来说却存在一个难题,即如何解释当Python遇到错误时显示的Traceback,有了这个功能就可以帮助用户快速解释错误消息。
举栗:
# author: 测试蔡坨坨
# datetime: 2022/10/29 15:35
# function: PEP 657: Fine-grained error locations in tracebacks
# 定义一个函数
def data():
return {
"name": "测试蔡坨坨",
"studying": [
{
"language": "Python",
"version": "3.11"
}
]
}
if __name__ == '__main__':
# 调用函数并指定一个错误的索引
print(data()["studying"][2])


一个IndexError的例子,我们可以看到嵌入在Traceback中的~和^符号详细地指向导致错误的代码,这种带注释的Traceback对于过于复杂的代码来说是比较友好的。
异常组
PEP 654: Exception Groups and except
官方文档:https://docs.python.org/3.11/whatsnew/3.11.html#whatsnew311-pep654
Exception Groups 让我们的Exception信息具有层次感,之前的Python每次只能处理一个Exception,异常组的使用丰富了Exception的作用,可以引入多个Exception,同时也丰富了我们代码分析的信息量。
举栗:
# author: 测试蔡坨坨
# datetime: 2022/10/29 15:58
# function: PEP 654: Exception Groups and except *
from builtins import ExceptionGroup
def test():
raise ExceptionGroup(
"异常嵌套关系",
[
ValueError(456),
ExceptionGroup(
"引入第三方库",
[
ImportError("无该模块"),
ModuleNotFoundError("你需要其他模组")
]
),
TypeError("int")
]
)
if __name__ == '__main__':
test()

支持TOML配置解析
PEP 680: tomllib — Support for parsing TOML in the Standard Library
官方文档:https://docs.python.org/3/library/tomllib.html#module-tomllib
增加了TOML(Tom's Obvious Minimal Language 的缩写)文件的读取,toml文件与自动化测试框架或Web开发中的config、yaml文件类似,都是通过修改配置文件来保持框架源码的不变。Python社区已将TOML作为首选格式,虽然TOML已被使用多年,但Python并没有内置的TOML支持,而在Python3.11中tomllib已经是个内置模块,这个新模块建立在 toml 第三方库之上,允许解析 TOML 文件。
举栗:
demo.toml:
# This is a TOML document.
title = "测试蔡坨坨"
[info]
name = "caituotuo"
blog = "www.caituotuo.top"
hobby = ["吃饭", "睡觉", "学Python"]
[other]
enable = true
# author: 测试蔡坨坨
# datetime: 2022/10/29 16:15
# function: PEP 680: tomllib — Support for parsing TOML in the Standard Library
import tomllib
def read_toml():
with open("demo.toml", "rb") as f:
data = tomllib.load(f)
print(data)
if __name__ == '__main__':
read_toml()
# {'title': '测试蔡坨坨', 'info': {'name': 'caituotuo', 'blog': 'www.caituotuo.top', 'hobby': ['吃饭', '睡觉', '学Python']}, 'other': {'enable': True}}
更加安全
PEP 675: Arbitrary literal string type
官方文档:https://docs.python.org/3/library/typing.html#typing.LiteralString
The new LiteralString annotation may be used to indicate that a function parameter can be of any literal string type. This allows a function to accept arbitrary literal string types, as well as strings created from other literal strings. Type checkers can then enforce that sensitive functions, such as those that execute SQL statements or shell commands, are called only with static arguments, providing protection against injection attacks.
防止SQL注入,指定参数为LiteralString,当传入普通的String时就不可以工作。
# author: 测试蔡坨坨
# datetime: 2022/10/29 16:34
# function: PEP 675: Arbitrary literal string type
from typing import LiteralString
def run_query(sql: LiteralString):
pass
def caller(arbitrary_string: str, literal_string: LiteralString) -> None:
run_query("SELECT * FROM students") # ok
run_query(literal_string) # ok
run_query("SELECT * FROM " + literal_string) # ok
run_query(arbitrary_string) # type checker error
run_query( # type checker error
f"SELECT * FROM students WHERE name = {arbitrary_string}"
)
异常Notes
PEP 678: Exceptions can be enriched with notes
官方文档:https://docs.python.org/3/whatsnew/3.11.html#whatsnew311-pep678
常规异常具有添加任意 notes 的扩展能力,可以使用.add_note()向任何异常添加一个note,并通过检查.__notes__属性来查看现有notes。
# author: 测试蔡坨坨
# datetime: 2022/10/29 16:53
# function: PEP 678: Exceptions can be enriched with notes
try:
raise TypeError('str')
except TypeError as e:
e.add_note('类型错了')
e.add_note('该用int')
raise e

以上几点就是我认为 Python 3.11 比较有意思的部分,更多内容可参考官方文档,想体验新功能的小伙伴感觉去试试新版本吧 ~
Python3.11正式版,它来了!的更多相关文章
- Windows 11正式版来了,下载、安装教程、一起奉上!
Windows 11正式版已经发布了,今天给大家更新一波Win11系统的安装方法,其实和Win10基本一样,有多种方法. 安装Win11前请先查看电脑是否支持Win11系统,先用微软自家的PC H ...
- 升级了 Windows 11 正式版,有坑吗?
今天磊哥去公司上班,惊喜的发现 Windows 提示更新了,并且是 Windows 11 正式版,这太让人开心了,二话不说"先升为敬". 下载更新 下载完咱就重启更新呗. Wi ...
- Windows 11 正式版 Build 22000.194 官方简体中文版、英文版(消费者版、商业版)下载
昨天阿三正式发布了 Windows 11,版本号竟然是 22000.194,也就是 9 月 16 日的 测试版 22000.194,仅仅是文件改了个名,特别是消费者版本 hash 校验都是一致的. W ...
- Linux Kernel 3.11 正式版发布
Linus 发布 了 3.11 版本的 Linux 内核.该版本值得关注的新特性有: Lustre 分布式文件系统.透明的 ARM 架构的大数据页支持:ARM64 上的 Xen 和 KVM 虚拟化:O ...
- macOS Big Sur 11.4 (20F71) 正式版(DMG、ISO、IPSW),百度网盘下载
本站提供的 macOS Big Sur 软件包,既可以拖拽到 Applications(应用程序)下直接安装,也可以制作启动 U 盘安装,或者在虚拟机中启动安装. 请访问原文链接:https://sy ...
- macOS Big Sur 11.3 (20E232) 正式版发布,百度网盘下载
本站提供的 macOS Big Sur 软件包,即可以直接拖拽到 Applications(应用程序)下直接安装,也可以制作启动 U 盘安装,或者直接在虚拟机中启动安装. 请访问原文链接:https: ...
- 制作正式版10.11 OS X El Capitan 安装U盘(优盘)
一.准备工作:1.准备一个 8GB 或以上容量的 U 盘,确保里面的数据已经妥善备份好(该过程会抹掉 U 盘全部数据)2.从官网Appstore下载下来的 “安装 OS X El Capitan”,当 ...
- iOS 11.4.1 正式版越狱
在 2018 年 Electra 最新能支持到 11.3.1 越狱,很长的一段时间 11.4 只能支持 Beta 版本,临近春节给了我们一个大礼物,终于支持 iOS 11.4-11.4.1,目前 iO ...
- MySQL 8.0 正式版 8.0.11 发布:比 MySQL 5.7 快 2 倍
ySQL 8.0 正式版 8.0.11 已发布,官方表示 MySQL 8 要比 MySQL 5.7 快 2 倍,还带来了大量的改进和更快的性能! 注意:从 MySQL 5.7 升级到 MySQL 8. ...
随机推荐
- Git 08 IDEA撤销添加
参考源 https://www.bilibili.com/video/BV1FE411P7B3?spm_id_from=333.999.0.0 版本 本文章基于 Git 2.35.1.2 如果将不想添 ...
- Linux 01 概述
参考源 https://www.bilibili.com/video/BV187411y7hF?spm_id_from=333.999.0.0 版本 本文章基于 CentOS 7.6 简介 Linux ...
- dentry的引用计数不对导致的crash
[17528853.189372] python invoked oom-killer: gfp_mask=0xd0, order=0, oom_score_adj=-998[17528853.189 ...
- 算法模板:spfa
#include<iostream> #include<algorithm> #include<cstring> #include<string> #i ...
- 【java】学习路线14-抽象类、多态
/*抽象类 abstractabstract class A{ }注意abstract类中不一定需要有abstract方法但是有abstract方法的类中,该类一定是abstract方法抽象类不 ...
- 简单创建一个SpringCloud2021.0.3项目(一)
目录 1. 项目说明 1. 版本 2. 用到组件 3. 功能 2. 新建父模块和注册中心 1. 新建父模块 2. 新建注册中心Eureka 3. 新建配置中心Config 4. 新建两个业务服务 1. ...
- 记录一次数据库CPU被打满的排查过程
1 前言 近期随着数据量的增长,数据库CPU使用率100%报警频繁起来.第一个想到的就是慢Sql,我们对未合理运用索引的表加入索引后,问题依然没有得到解决,深入排查时,发现在 order by id ...
- Knative部署应用以及应用的更新、应用的分流(二)
1. 应用的更新 1.1 更新hello-example应用 1.更新应用的环境变量 可通过命令行的方式亦可以通过读取配置文件的方式,这里主要来看命令行的方式 [root@kn-server-mast ...
- KingbaseES OUT 类型参数过程与函数的调用方法
对于含有 out 类型参数的过程或者函数,只能通过块方式调用,这是因为,ksql 还不支持类似 Oracle 那样通过 var 定义变量. 一.带OUT的procedure 调用 创建过程: crea ...
- QT学习(三)
首先整理一下编码的方法.对于一个待解决的问题,首先应该将大问题分解成小问题,将小问题划分为小小问题... 然后再进行类的抽象,将划分成的问题和类进行对应.然后再对划分的小..问题进行具体的处理分析,划 ...