跳转到正文

Python 3.12 ​

参考官方文档: Python 3.12 Official Release Notes
Python 3.12 引入了 PEP 695 类型参数语法,并通过 PEP 701 统一了 F-String 的语法规则。


🐳 容器运行环境 (Runtime Environment) ​

在标准 Docker 镜像 python:3.12-slim 中执行控制台诊断指令 python --version:

📸 Unverified Snapshot📋python:3.12-slim
⏱️ 623msNot verified
Python 3.12.13

1. 🔷 PEP 695:类型参数语法 ​

允许直接在类与函数名后使用 [T] 声明泛型类型,免去 TypeVar 繁琐定义。

python
# 关联源码: demos/python/type_param_demo.py
class Stack[T]:
    def __init__(self) -> None:
        self.items: list[T] = []

    def push(self, item: T) -> None:
        self.items.append(item)
📸 Unverified Snapshot📋python:3.12-slim
⏱️ 875msNot verified
Generic Stack[int]: [10, 20, 30]
Popped: 30
Stack type: <class 'int'>

2. 📝 PEP 701: 规范化 F-Strings 语法 (Formalized F-Strings) ​

取消了内部引号嵌套限制、表达式反斜杠限制与多行表达式限制。

python
# 关联源码: demos/python/fstring_demo.py
user = {"name": "Alice"}
result = f"User: {user['name']!r}, List: {', '.join(['a', 'b'])}"
📸 Unverified Snapshot📋python:3.12-slim
⏱️ 517msNot verified
Formalized F-String Demo:
f"User: {user['name']!r}, List: {', '.join(['a', 'b'])}"
Output: User: 'Alice', List: a, b

版本信息与迁移 ​

  • 发布时间 / 标准时间: 2023 年 10 月
  • 维护状态: 截至 2026-08-27,以页面所链接的官方生命周期或规范状态为准
  • 运行时或平台基线: CPython 解释器、ABI、虚拟环境和项目依赖支持范围

迁移影响: 升级前阅读 What’s New 与 Porting 文档,重建虚拟环境和二进制扩展,处理弃用与移除 API,并用目标解释器执行完整测试。

版本确认 ​

bash
python --version
python -c "import sys, platform; print(sys.version); print(platform.platform())"

资料核对日期:2026-08-27。

Released under the MIT License.