Python 3.12 官方 Release Notes 深度拆解大典
参考官方文档: Python 3.12 Official Release Notes
Python 3.12 是现代化 CPython 性能与类型系统大成的版本。它带来了 PEP 695 极简泛型语法 以及 PEP 701 彻底规范化的 F-Strings。
🐳 容器运行环境 (Runtime Environment)
在标准 Docker 镜像 python:3.12-slim 中执行控制台诊断指令 python --version:
📸 Unverified Snapshot📋
python:3.12-slim⏱️ 623msNot verified
Python 3.12.131. 🔷 PEP 695: 极简泛型语法 (Type Parameter Syntax)
允许直接在类与函数名后使用 [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