Python 3.11 新加入的和类型系统相关的新特性

PEP 646 – Variadic Generics 介绍这个 PEP 之前需要补一些知识,我们逐步深入先了解一下泛型 (Generics)。 泛型是指在定义函数或类的时候,不预先指定具体的类型,而在使用的时候再指定类型的一种特性。对于 Python 这种动态语言,因为一切都是对象引用,可以在使用时直接判断类型:In : def say_type(obj):...: match obj:...: case int():...: print('int')...: case str():...: print(……