gpt4 book ai didi

python - 有没有办法在其以前的版本中使用 Python 3.9 类型提示?

转载 作者:行者123 更新时间:2023-12-03 16:32:38 25 4
gpt4 key购买 nike

在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com