在 Python 3.9 中,我们可以按照 here 所述,以小写内置方式使用类型提示(无需从 typing
模块导入类型签名)。 :
def greet_all(names: list[str]) -> None:
for name in names:
print("Hello", name)
我非常喜欢这个想法,我想知道是否可以使用这种类型提示的方式,但是在以前的 python 版本中,例如 Python 3.7,我们编写了这样的类型提示:from typing import List
def greet_all(names: List[str]) -> None:
for name in names:
print("Hello", name)
最佳答案
简单地说,导入 annotations
来自 __future__
你应该很高兴。
from __future__ import annotations
import sys
!$sys.executable -V #this is valid in iPython/Jupyter Notebook
def greet_all(names: list[str]) -> None:
for name in names:
print("Hello", name)
greet_all(['Adam','Eve'])
Python 3.7.6
Hello Adam
Hello Eve
关于python - 有没有办法在其以前的版本中使用 Python 3.9 类型提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63939138/