【问题标题】:What does vertical bar "|" (pipe) in function arguments type annotations mean?竖线“|”是什么意思(管道)在函数参数类型注释中是什么意思?
【发布时间】:2020-10-30 21:25:54
【问题描述】:

我遇到了这样签名的函数:

def get_quantile(numbers: List[float], q: float | int ) -> float | int | None :

什么意思?

这是我的 python 3.8 上的语法错误。我是否需要从 future 导入某些内容才能使其工作?

【问题讨论】:

标签: python python-3.x type-hinting type-annotation


【解决方案1】:

根据PEP 604| 将用于指定 Python 3.10 中的联合类型。

所以float | int 将表示Union[float, int],即浮点数或整数。

【讨论】:

    【解决方案2】:

    意思是or。所以q: float | int 意味着q 可能是floatint

    【讨论】:

    • 您确定or 可以在注释中吗?除了一些奇怪的类型和值计算。很抱歉攻击你的答案,但你让“或”看起来像一个 Python 关键字,可能会误导某人;)
    猜你喜欢
    • 2012-04-27
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    • 1970-01-01
    • 2016-10-17
    相关资源
    最近更新 更多