Python 3.10 新加入的四个和类型系统相关的新特性

本文详细介绍在 Python 3.10 新加入的四个和类型系统相关的新特性。PEP 604: New Type Union Operator 在之前的版本想要声明类型包含多种时,这么写:from typing import Uniondef square(number: Union[int, float]) -> Union[int, float]: return number ** 2 这么写其实比较麻烦,每次都要 from typing import Union 再使用 Union[],现在直接可以使用|来表示:def square(number: int | float) -> i……