卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16333本站已运行3317

Python 3.10版本中新增了哪些新功能?

Python 3.10版本中新增了哪些新功能?

In this article, we will learn the new features in Python 3.10, compared to 3.9. Let’s see the features −

Parenthesized context managers

现在支持使用括号来跨多行继续使用上下文管理器。这样可以以与之前的导入语句相似的方式,将长的上下文管理器集合格式化为多行。

User-Defined Type Guards

TypeGuard has been added to the typing module to annotate type guard functions and improve information provided to static type checkers during type narrowing.

Enhanced error messages

如果您在运行Python程序时遇到错误,错误消息现在将更准确,并提供确切的错误信息

SyntaxError

的中文翻译为:

SyntaxError

在解析包含未闭合括号或方括号的代码时,解释器现在会包括未闭合括号或方括号的位置,而不是显示SyntaxError: unexpected EOF while parsing或指向错误的位置

SyntaxError exceptions raised by the interpreter will now highlight the full error range of the expression that constitutes the syntax error itself, instead of just where the problem is detected.

IndentationErrors

的翻译为:

缩进错误

许多IndentationError异常现在提供了更多关于期望缩进的块类型的上下文信息

精确的行号用于调试

More precise and reliable line numbers for debugging, profiling and coverage tools. Tracing events, with the correct line number, are generated for all lines of code executed and only for lines of code that are executed.

Structural Pattern Matching

已经通过匹配语句和带有关联操作的模式的case语句添加了结构模式匹配。模式包括序列、映射、原始数据类型以及类实例。模式匹配使程序能够从复杂的数据类型中提取信息,根据数据的结构进行分支,并根据不同形式的数据应用特定的操作。

增强模块

以下模块增加了新功能、新方法等。

array − The index() method of array.array now has optional start and stop parameters.

base64 − Added base64.b32hexencode() and base64.b32hexdecode() to support the Base32 Encoding with Extended Hex Alphabet.

bisect − Added the possibility of providing a key function to the APIs in the bisect module.

contextlib − 添加了一个contextlib.aclosing()上下文管理器,用于安全关闭异步生成器和表示异步释放资源的对象。

distutils − The distutils package is deprecated, to be removed in Python 3.12.

encodings − The encodings.normalize_encoding() now ignores non-ASCII characters.

卓越飞翔博客
上一篇: 如何在Python中计算CSV文件中的行数?
下一篇: Python程序递归线性搜索数组中的元素
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏