Python 3.11 新特性全面总结

Python 3.11 新特性全面总结

发布时间:2022 年 10 月 24 日
官方文档:https://docs.python.org/zh-cn/3.11/whatsnew/3.11.html


一、性能大幅提升

Faster CPython 项目

Python 3.11 比 3.10快 10-60%,标准基准测试平均提速1.25x

主要优化方向:

  • 启动时间优化
  • 更快的函数调用
  • 更快的对象创建
  • 更高效的字典操作
  • 内联缓存优化

这是 Python 近年来最显著的性能提升,后续版本(3.12、3.13)将继续深化优化。


二、重磅新语法

1. 异常组与except*(PEP 654)

支持同时抛出和处理多个无关异常,适合并发、批量操作场景:

# 抛出多个异常exceptions=[ValueError("Invalid value"),TypeError("Wrong type"),RuntimeError("Failed")]raiseExceptionGroup("Multiple errors",exceptions)# 使用 except* 处理异常组try:...except*ValueErroraseg:print(f"ValueError组:{eg.exceptions}")except*TypeErroraseg:print(f"TypeError组:{eg.exceptions}")

异常组特性:

  • ExceptionGroup/BaseExceptionGroup:异常组类型
  • except*:匹配异常组的子组
  • 支持嵌套异常组
  • eg.exceptions:访问组内所有异常

应用场景:

  • 并发任务中多个任务失败
  • 批量操作部分失败
  • 多重校验错误收集

2. 异常附加备注(PEP 678)

为异常添加上下文信息,不改变异常类型:

try:process_data(data)exceptExceptionase:e.add_note(f"处理文件{filename}时失败")e.add_note(f"数据大小:{len(data)}")raise# 输出示例# Traceback (most recent call last):# ...# ValueError: Invalid data# 处理文件 data.json 时失败# 数据大小: 1024

用途:

  • 添加调试信息
  • 记录上下文(文件名、行号、用户输入等)
  • 不需要自定义异常类

三、错误定位精确化(PEP 657)

Traceback 现在指向精确的表达式,而非整行:

# 之前(3.10)Traceback(most recent call last):File"distance.py",line6,inmanhattan_distancereturnabs(point_1.x-point_2.x)+abs(point_1.y-point_2.y)AttributeError:'NoneType'objecthas no attribute'x'# 现在(3.11)Traceback(most recent call last):File"distance.py",line6,inmanhattan_distancereturnabs(point_1.x-point_2.x)+abs(point_1.y-point_2.y)^^^^^^^^^AttributeError:'NoneType'objecthas no attribute'x'

复杂表达式示例:

# 字典访问链return1+query_count(db,response['a']['b']['c']['user'],retry=True)~~~~~~~~~~~~~~~~~~^^^^^TypeError:'NoneType'objectisnotsubscriptable# 算术表达式result=(x/y/z)*(a/b/c)~~~~~~^~~ZeroDivisionError:division by zero

禁用精确位置(减少内存占用):

python-Xno_debug_ranges script.py# 或PYTHONNODEBUGRANGES=1python script.py

四、新增标准库模块

1.tomllib— TOML 解析(PEP 680)

标准库终于内置 TOML 支持:

importtomllibwithopen('config.toml','rb')asf:# 注意:必须二进制模式config=tomllib.load(f)# 或从字符串解析config=tomllib.loads(""" [database] host = "localhost" port = 5432 """)

注意:tomllib只支持读取,写入需使用第三方库如tomli-w


五、标准库重要改进

1.asyncio.TaskGroup— 结构化并发

推荐的新并发模式,替代create_task()+gather()

importasyncioasyncdefmain():asyncwithasyncio.TaskGroup()astg:task1=tg.create_task(fetch_data(url1))task2=tg.create_task(fetch_data(url2))task3=tg.create_task(fetch_data(url3))# TaskGroup 退出时自动等待所有任务完成print(task1.result(),task2.result(),task3.result())

优势:

  • 自动等待所有任务
  • 自动传播异常(使用 ExceptionGroup)
  • 更清晰的结构化并发

2.asyncio.timeout()— 超时上下文管理器

importasyncioasyncdefmain():try:asyncwithasyncio.timeout(10.0):awaitlong_running_operation()exceptTimeoutError:print("操作超时")

3.asyncio.Barrier— 同步原语

importasyncioasyncdefworker(barrier,worker_id):print(f"Worker{worker_id}准备就绪")awaitbarrier.wait()print(f"Worker{worker_id}开始执行")asyncdefmain():barrier=asyncio.Barrier(3)asyncwithasyncio.TaskGroup()astg:foriinrange(3):tg.create_task(worker(barrier,i))

4.datetime.UTC— 时区常量

fromdatetimeimportdatetime,UTC dt=datetime.now(UTC)# 更简洁# 等价于 datetime.now(timezone.utc)

5.datetime.fromisoformat()增强

现在支持解析更多 ISO 8601 格式:

fromdatetimeimportdatetime,date,time datetime.fromisoformat('2024-01-15T10:30:45+08:00')date.fromisoformat('2024-01-15')time.fromisoformat('10:30:45+08:00')

6.math模块新增函数

importmath math.exp2(3)# 2^3 = 8.0math.cbrt(27)# 立方根 = 3.0

7.enum模块大幅增强

fromenumimportEnum,StrEnum,Flag,auto# StrEnum:成员必须是字符串classColor(StrEnum):RED="red"GREEN="green"BLUE="blue"# Flag 增强:支持 len()、迭代、inclassPerm(Flag):R=auto()W=auto()X=auto()p=Perm.R|Perm.Wlen(p)# 2list(p)# [Perm.R, Perm.W]Perm.Rinp# True

8.hashlib.file_digest()— 文件哈希

importhashlibwithopen('large_file.bin','rb')asf:digest=hashlib.file_digest(f,'sha256')print(digest.hexdigest())

9.functools.singledispatch支持联合类型

fromfunctoolsimportsingledispatch@singledispatchdefprocess(arg):print(f"默认:{arg}")@process.registerdef_(arg:int|float):print(f"数值:{arg}")@process.registerdef_(arg:list|set):print(f"集合:{arg}")

10.re支持原子分组和占有量词

importre# 原子分组 (?>...)re.match(r'(?>a+)b','aaab')# 匹配失败时不会回溯# 占有量词 *+、++、?+re.match(r'a*+b','aaab')# 占有式匹配

六、类型注解改进

1. 变长泛型(PEP 646)

支持*Ts语法,用于不定长类型参数:

fromtypingimportTypeVarTuple Ts=TypeVarTuple('Ts')classArray:def__getitem__(self,index:tuple[*Ts])->Array[*Ts]:...

2.Self类型(PEP 673)

精确标注返回自身类型的方法:

fromtypingimportSelfclassShape:defset_scale(self,scale:float)->Self:self.scale=scalereturnselfclassCircle(Shape):defset_radius(self,radius:float)->Self:self.radius=radiusreturnself

3. TypedDict 字段必需性标记(PEP 655)

fromtypingimportTypedDict,Required,NotRequiredclassPerson(TypedDict):name:Required[str]# 必需age:NotRequired[int]# 可选email:str# 根据 total 决定

4. 类型检查辅助函数

fromtypingimportassert_never,reveal_type,assert_type,Never# assert_never:确认代码不可达(类型检查器验证)defhandle(value:int|str):ifisinstance(value,int):...elifisinstance(value,str):...else:assert_never(value)# 类型检查器会报错如果有遗漏# reveal_type:查看类型检查器推断的类型reveal_type(x)# 类型检查器会输出推断的类型# assert_type:验证类型推断assert_type(x,int)# Never:永不返回的类型defnever_return()->Never:raiseRuntimeError()

5.typing.Any支持子类化

fromtypingimportAnyclassDynamicObject(Any):"""用于 mock 等高度动态的场景"""pass

七、其他语言改进

1.for循环支持星号解包

forx,*restin[(1,2,3),(4,5,6,7)]:print(x,rest)# 1 [2, 3]# 4 [5, 6, 7]

2. 异步推导式嵌套

asyncdefprocess():# 外层推导式自动变为异步results=[xasyncforxingen()ifawaitcheck(x)]

3.-P选项与PYTHONSAFEPATH

防止当前目录污染模块导入:

python-Pscript.py# 或PYTHONSAFEPATH=1python script.py

这会禁止自动将脚本目录或当前目录添加到sys.path,避免恶意模块覆盖。

4. 格式化字符串新增z选项

将负零转为正零:

f"{-0.0:.2f}"# '-0.00'f"{-0.0:.2fz}"# '0.00'

5. 整数字符串转换限制

防止 DoS 攻击,默认限制 4300 位:

# 超过 4300 位会抛出 ValueErrorint('1'*5000)# ValueError# 可通过环境变量调整# PYTHONINTMAXSTRDIGITS=0 python script.py # 禁用限制

八、弃用与移除

重要弃用(PEP 594)

大量遗留标准库模块被弃用,将在Python 3.13移除:

弃用模块说明替代方案
cgicgitbCGI 支持使用 WSGI 框架
audioop音频操作使用第三方库
imghdr图像类型识别使用puremagicfiletype
mailcapMIME 类型使用mimetypes
msilibWindows 安装器使用第三方工具
telnetlibTelnet 协议使用telnetlib3
xdrlibXDR 编码使用construct

正式移除

移除内容说明
Py_UNICODE编码器 APIPEP 624
bytessys.path3.6 起已失效

九、其他值得关注的变化

时间精度提升

  • Unixtime.sleep()使用clock_nanosleep(),精度 1 纳秒
  • Windows 8.1+time.sleep()使用高精度定时器,精度 100 纳秒

线程锁使用单调时钟

threading.Lock.acquire()使用CLOCK_MONOTONIC,不受系统时间变化影响。

sqlite3增强

  • 异常包含 SQLite 扩展错误码:sqlite_errorcodesqlite_errorname
  • 新增serialize()/deserialize():数据库序列化与反序列化
  • 新增blobopen():增量 I/O 操作
  • 新增create_window_function():窗口函数

unittest支持上下文管理器

importunittestclassMyTest(unittest.TestCase):deftest_something(self):withself.enterContext(some_resource())asr:# 资源自动清理...

总结

Python 3.11 的核心亮点:

  1. 性能提升 10-60%— 近年来最显著的提速
  2. 异常组ExceptionGroup+except*— 处理多个并发异常
  3. 精确错误定位— Traceback 指向具体表达式
  4. 异常附加备注— 丰富错误上下文
  5. tomllib— 标准库内置 TOML 解析
  6. asyncio.TaskGroup— 结构化并发新模式
  7. Self类型 — 精确标注返回自身类型

Python 3.11 是一个性能与开发体验双提升的版本,错误定位改进让调试更轻松,异常组让并发错误处理更优雅,性能提升让 Python 在更多场景下保持竞争力。


参考:Python 3.11 官方文档 - What’s New
内容由 AI 整理生成,内容仅供参考,请仔细甄别。