在Python 3.9,我们可以使用类型提示中内置的方式小写(而不必输入型特征从typing模块)描述在这里:
def greet_all(names: list[str]) -> None:
for name in names:
print("Hello", name)
Run Code Online (Sandbox Code Playgroud)
我非常喜欢这个想法,我想知道是否可以使用这种类型提示的方式,但是在以前的 Python 版本中,例如 Python 3.7,我们编写了这样的类型提示:
from typing import List
def greet_all(names: List[str]) -> None:
for name in names:
print("Hello", name)
Run Code Online (Sandbox Code Playgroud)