Python 中如何声明数组类型提示?

How are array typehints declared in Python?

给出这个函数:

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

我如何在不导入 List(并使用 List[str] 语法)的情况下键入此函数 returns 字符串数组的提示?

这就是您要查找的内容:

from typing import List

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

你可以这样做

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