惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Threat Research - Cisco Blogs
A
Arctic Wolf
S
Securelist
O
OpenAI News
T
Threatpost
Forbes - Security
Forbes - Security
N
News and Events Feed by Topic
S
Secure Thoughts
H
Heimdal Security Blog
S
Security Affairs
P
Privacy International News Feed
C
Cisco Blogs
C
CERT Recently Published Vulnerability Notes
Cyberwarzone
Cyberwarzone
N
News and Events Feed by Topic
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Palo Alto Networks Blog
S
Security @ Cisco Blogs
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
T
Tailwind CSS Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hacker News: Ask HN
Hacker News: Ask HN
T
Troy Hunt's Blog
S
SegmentFault 最新的问题
腾讯CDC
V
Visual Studio Blog
Last Week in AI
Last Week in AI
H
Hacker News: Front Page
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Project Zero
Project Zero
WordPress大学
WordPress大学
NISL@THU
NISL@THU
博客园 - 【当耐特】
博客园 - Franky
Webroot Blog
Webroot Blog
博客园_首页
T
Tenable Blog
雷峰网
雷峰网
Google Online Security Blog
Google Online Security Blog
阮一峰的网络日志
阮一峰的网络日志
V2EX - 技术
V2EX - 技术
Recent Commits to openclaw:main
Recent Commits to openclaw:main
L
Lohrmann on Cybersecurity
The Hacker News
The Hacker News

博客园 - 悠哉大斌

AI Agent 安全沙箱技术对比:E2B、Cube Sandbox 与 Docker Sandboxes linux 升级 claude code的坑 debian(WSL) apt 代理配置 linux内核之Namespaces、Cgroups、Capabilities、Seccomp和Landlock 大语言模型推理中的隐藏瓶颈及其解决方法 Docker Sandboxes 技术是什么,和docker容器有什么区别 什么是 Docker Agent以及一个Docker Agent示例 windows wsl 安装 Temurin® JDK claude Code 和 codex 作为编程代理,前者使用typescript开发,后者使用rust,他们的决策依据是什么 Java 并发编程发展史与 java.util.concurrent 全景解析 LLM 真的改变了编码风格吗? 如果人工智能会犯错且不精确,为什么它正在改变世界? Python 的协程模型和 JavaScript 的 async/await python闭包和function.__closure__特殊属性 python 的 dunder name和 sunder name 基于nodejs设计REST API的知名开源框架 windows上使用node-oracledb Thick 模式连接 Oracle 11g Claude Code CLI连接 DeepSeek V4模型后调用报400错误 python里对象(object)到底是什么 Python 类型别名的演变 Python 类型提示的演变史 PEP 593 新增的Annotated 类型 Alibaba AgentScope 和 microsoft agent framework 详细对比分析 spring AI Alibaba Agent Framework 和 agentscope有什么区别和联系 AI Agent协作模式以及主流开源框架对协作模式的支持 Tailscale 是如何接管 DNS 的? Agent = Model + Harness 使用rust编写typescript编译器的难点在什么地方,哪些数据结构是rust不擅长的? TypeScript/JavaScript 中的异步迭代语句 js中的生成器函数 Tailscale Serve and Funnel openclaw gateway的网络绑定模式 websocket协议和http协议有何依赖关系? MCP通信的双方是谁? claude code MCP 安装范围 如何在wsl2环境下给claude code cli 配置 playwright-mcp wsl的网络模式有哪几种,有哪些区别? AI Agent memory是什么? ai agent skills是什么? Go测试生态系统工具与最佳实践深度调研(聚焦认证授权系统) 线性代数中常见矩阵类型的概念关系思维导图 三种主流授权策略
Python 泛型演变史
悠哉大斌 · 2026-04-23 · via 博客园 - 悠哉大斌

Python 泛型(Generics)的演变几乎就是 Python 类型系统演变的主线

甚至可以说:

类型提示的发展 = 泛型能力不断增强。

它大概经历了五代。


Python 泛型演变史

一、没有泛型的时代

最早只能写:

def first(items):
    return items[0]

问题:

  • 输入元素类型丢失
  • 返回类型无法关联输入

比如:

first([1,2,3])  # 应该推断 int

早期做不到。


二、PEP 484:TypeVar 时代(第一代泛型)

PEP 484

这是经典写法:

from typing import TypeVar

T = TypeVar("T")

def first(items: list[T]) -> T:
    return items[0]

现在:

输入是 list[int]

返回自动是:

int

这是参数化多态

其实就是:

f : T -> T

identity function。

def identity(x: T) -> T:
    return x

非常经典。


约束泛型

T = TypeVar("T", int, float)

只允许:

  • int
  • float

上界

T = TypeVar("T", bound=str)

必须是 str 子类。


泛型类

from typing import Generic

T = TypeVar("T")

class Box(Generic[T]):
    def __init__(self, value: T):
        self.value = value

使用:

Box[int]

这是 Python 泛型正式诞生。


三、PEP 585:内置泛型时代

PEP 585

旧:

List[T]

新:

list[T]

这件事意义比看起来大。

因为:

list

自己变成 generic class。

这很像:

List<T>

只是 Python 化。


嵌套泛型更舒服

dict[str, list[int]]

自定义泛型仍旧笨重

问题还在:

class Box(Generic[T]):

还是冗长。

所以还有下一步。


四、高阶泛型时代

这是高手区。


ParamSpec

ParamSpec

普通 TypeVar 只能描述值类型。

描述不了:

函数参数列表。


装饰器以前很难写:

def log(fn):
    ...

会丢签名。


有 ParamSpec:

from typing import ParamSpec
from collections.abc import Callable

P = ParamSpec("P")
T = TypeVar("T")

def log(fn: Callable[P, T]) -> Callable[P, T]:
    ...

参数原封保留。

这是二阶泛型。


TypeVarTuple

更疯狂。

Ts = TypeVarTuple("Ts")

可变长度泛型:

tuple[*Ts]

比如:

tuple[int, str, float]

都能表达。

像 C++ variadic templates。


五、PEP 695:新泛型语法(革命)

PEP 695

这是最大飞跃。


函数泛型

旧:

T = TypeVar("T")

def identity(x: T) -> T:

新:

def identity[T](x: T) -> T:

像 TypeScript:

function identity<T>(x:T): T

类泛型

旧:

class Box(Generic[T]):

新:

class Box[T]:

终于不需要:

  • Generic
  • TypeVar

泛型别名

type Vec[T] = list[T]

漂亮得不像 Python。


类型参数约束

def add[T: (int, float)](x:T, y:T) -> T:

约束直接放签名。


这五代变化本质是什么

第一代

“类型参数存在了”

TypeVar

第二代

“标准库类型成为泛型”

list[T]

第三代

“高阶泛型”

ParamSpec
TypeVarTuple

第四代

“语法内建泛型”

def foo[T]()

第五代(正在逼近)

开始有点像轻量 dependent typing。

例如:

  • Literal
  • TypeGuard
  • Variadic generics

越来越像现代类型系统。


一个很有趣的暗线

Python 泛型其实在逐渐摆脱 Java 风格。

早期:

很像 Java:

class Box<T>

后来越来越像 TypeScript / Rust:

type Result[T] = T | Error

风格变了。


对照图

年代 泛型能力
pre-484
PEP 484 TypeVar
PEP 585 内置泛型
ParamSpec时代 高阶泛型
PEP 695 原生泛型语法