竖线“|”是什么意思(pipe) in function arguments type annotations 是什么意思?
What does vertical bar "|" (pipe) in function arguments type annotations mean?
我遇到了带有这样签名的函数:
def get_quantile(numbers: List[float], q: float | int ) -> float | int | None :
这是什么意思?
这是我的 python 3.8 的语法错误。我是否需要从 future
导入一些东西才能让它工作?
根据 PEP 604,|
将用于指定 Python 3.10 中的联合类型。
因此 float | int
将表示 Union[float, int]
,即浮点数或整数。
表示or
。所以 q: float | int
意味着 q
可能是 float
或 int
.
我遇到了带有这样签名的函数:
def get_quantile(numbers: List[float], q: float | int ) -> float | int | None :
这是什么意思?
这是我的 python 3.8 的语法错误。我是否需要从 future
导入一些东西才能让它工作?
根据 PEP 604,|
将用于指定 Python 3.10 中的联合类型。
因此 float | int
将表示 Union[float, int]
,即浮点数或整数。
表示or
。所以 q: float | int
意味着 q
可能是 float
或 int
.