【问题标题】:How are array typehints declared in Python? [duplicate]Python 中如何声明数组类型提示? [复制]
【发布时间】:2020-04-01 23:06:55
【问题描述】:

给定这个函数:

def array_returner():
    return ["hello", "there"]

在不导入 List(并使用 List[str] 语法)的情况下,我如何输入提示该函数返回字符串数组?

【问题讨论】:

标签: python type-hinting


【解决方案1】:

这就是你要找的东西:

from typing import List

def array_returner() -> List[str]:
    return ["hello", "there"]

【讨论】:

    【解决方案2】:

    你可以这样做

    • def array_returner() -> [str]:
    • def array_returner() -> List[str]: (from typing import List)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      • 2020-01-01
      • 2014-10-10
      • 2023-04-09
      • 2015-02-20
      • 2019-08-12
      相关资源
      最近更新 更多