Typing
概念基础
from typing import List, Tuple, Dict def test(a:int, s:str, f:float, b:bool) -> Tuple[int, Tuple, Dict, bool]: l = a tup = tuple(s) di = {'key': f} bo = b return l, tup, di, bo print(test(12, 'test', 1.00, 1)) # 输入正确类型的参数 # 结果 (12, ('t', 'e', 's', 't'), {'key': 1.0}, 1) print(test('sss', 'test', 1, 'h')) # 输入错误的参数类型 # 结果 ValueError: invalid literal for int() with base 10: 'sss' # 注意:报错并不是因为typing检查到了类型错误,而是里面的int方法不支持对str进行操作
常见类型
实例
Variable
Built-in Type
Function
Mixed Structure
lambda 的类型标注
鸭子类型
Class
NoReturn
Dict、Mapping、MutableMapping
Sequence
Any
TypeVar
NewType
Union
Optional
Last updated
