7

在 Python 3.9 中,我们可以使用小写内置方式的类型提示(无需从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)
4

1 回答 1

6

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
于 2020-09-17T13:38:06.947 回答