kbn*_*bnt 5 python static-typing type-hinting python-3.9
在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)
ale*_*inn 14
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1409 次 |
| 最近记录: |