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

New*_*yte 7 python type-hinting

给定这个函数:

def array_returner():
    return ["hello", "there"]
Run Code Online (Sandbox Code Playgroud)

我如何输入提示该函数返回一个字符串数组,而不导入 List (并使用List[str]语法)?

And*_*ndy 13

这就是您正在寻找的:

from typing import List

def array_returner() -> List[str]:
    return ["hello", "there"]
Run Code Online (Sandbox Code Playgroud)


azr*_*zro 10

你可以这样做

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