目录typing模块Dict List Tuple set/AbstractSet Sequence NoReturn Any TypeVar NewType Callable Union Optional Generator总结function annotation 写法:使用冒号:加类型代表参数类型
默认值参数示例:
b: int = 2使用->加类型代表返回值类型python解释器运行时并不会检查类型,类型不对也不会抛异常,仅仅是注解而已。示例:def plus(a: int, b: int = 2) -> int:return a + bpython 解析器并不会在意类型注解,严格来说这是不对的,Python 会把类型信息放在__annotations__
属性中:
>>> def foo(a: str):... print('hello', a)...
>>> foo.__annotations__{'a': str}
>>> class Bar:... a: str... b: int
>>> Bar.__annotations__{'a': str, 'b': int}typing模块
内置提供的类型:
int、str、float,typing模块提供的类型:Dict、List、Tuble...
typing使用方括号Dict[str, int]而不是圆括号Dict(str, int)Dict Dict[str, int]: 表示一个 keys 的类型为 str,values 的类型为 int 的字典,比如 {"a": 1, "b": 2}from typing import Dict Dict[str, Dict[str, List[str]]]如下:{'原木镇': {'第一小学': ['张伟', '王伟', '王芳'],'第二小学': ['李伟', '李娜'],},'鸽子镇': {'高山中学': ['张敏', '李静'],'亿百中学': ['王静']'蟒蛇小学': ['刘伟', '王秀英']}}List List[int] 表示由整型组成的列表,比如[0, 1, 1, 2, 3]List[List[int]] = [[1, 2], [2, 3]]Tuple Tuple[int, float, str] is a tuple of an int, a float and a string.
person: Tuple[str, int, float] = ('Mike', 22, 1.75)set/AbstractSet根据官方文档,Set 推荐用于注解返回类型,AbstractSet 用于注解参数def describe(s: AbstractSet[int]) -> Set[int]:return set(s)Sequence Sequence,是 collections.abc.Sequence 的泛型,在某些情况下,我们可能并不需要严格区分一个变量或参数到底是列表 list 类型还是元组 tuple 类型,我们可以使用一个更为泛化的类型,叫做 Sequence,其用法类似于 Listdef square(elements: Sequence[float]) -> List[float]:return [x ** 2 for x in elements]NoReturn NoReturn,当一个方法没有返回结果时,为了注解它的返回类型,我们可以将其注解为NoReturndef hello() -> NoReturn:print('hello')Any Any,可以代表所有类型,所有的无参数类型注解和返回类型注解的都会默认使用 Any 类型,以下两个函数等价:def add(a):return a + 1
def add(a: Any) -> Any:return a + 1TypeVar TypeVar,自定义兼容特定类型的变量,比如有的变量声明为 int、float、None 都是符合要求的,实际就是代表任意的数字或者空内容都可以,其他的类型则不可以,比如列表 list、字典 dict 等等,像这样的情况,我们可以使用 TypeVar 来表示。
height = 1.75 Height = TypeVar('Height', int, float, None) def get_height() -> Height:return heightNewType newType,声明一些具有特殊含义的类型,像 Tuple 的例子一样,我们需要将它表示为 Person,即一个人的含义,但但从表面上声明为 Tuple 并不直观,所以我们可以使用 NewType 为其声明一个类型,如:Person = NewType('Person', Tuple[str, int, float]) person = Person(('Mike', 22, 1.75))实际上 person 就是一个 tuple 类型,我们可以对其像 tuple 一样正常操作。
Callable Callable,可调用类型,通常用来注解一个方法, 在声明的时候需要使用Callable[[Arg1Type, Arg2Type, ...], ReturnType]这样的类型注解,将参数类型和返回值类型都要注解出来,例如:def date(year: int, month: int, day: int) -> str:return f'{year}-{month}-{day}'
def get_date_fn() -> Callable[[int, int, int], str]:return date-> Callable[[int, int, int], str]: 中括号内分别标记了返回的方法的参数类型和返回值类型。
Union Union,联合类型,Union[X, Y]代表要么是 X 类型,要么是 Y 类型。
Union[Union[int, str], float] == Union[int, str, float]Union[int] == int Union[int, str, int] == Union[int, str]# 无参数顺序Union[int, str] == Union[str, int]在一些方法参数声明的时候比较有用,比如一个方法,要么传一个字符串表示的方法名,要么直接把方法传过来:def process(fn: Union[str, Callable]):if isinstance(fn, str):# str2fn and process pass elif isinstance(fn, Callable):fn()这样的声明在一些类库方法定义的时候十分常见。
Optional Optional,意思是说这个参数可以为空或已经声明的类型,即Optional[X]等价于Union[X, None]。
Optional 并不等价于可选参数,当它作为参数类型注解的时候,不代表这个参数可以不传递,而是说这个参数可以传None,不传也会报错。
当一个方法执行结果,如果执行完毕就不返回错误信息, 如果发生问题就返回错误信息,则可以这么声明:def judge(result: bool) -> Optional[str]:if result: return 'Error Occurred'Generator Generator,想代表一个生成器类型,可以使用 Generator,它的声明比较特殊,其后的中括号紧跟着三个参数,分别代表 YieldType、SendType、ReturnType,如:def echo_round() -> Generator[int, float, str]:sent = yield 0 while sent >= 0:sent = yield round(sent) return 'Done'在这里 yield 关键字后面紧跟的变量的类型就是 YieldType,yield 返回的结果的类型就是 SendType,最后生成器 return 的内容就是 ReturnType。
当然很多情况下,生成器往往只需要 yield 内容就够了,我们是不需要 SendType 和 ReturnType 的,可以将其设置为空,如:def infinite_stream(start: int) -> Generator[int, None, None]:while True:yield start start += 1总结本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!您可能感兴趣的文章:Python中typing模块与类型注解的使用方法Python 中的 typing 模块常见用法Python中typing模块的具体使用Python类型注解必备利器typing模块全面解读python typing模块--类型提示支持使用Python Typing模块提升代码可读性和健壮性实例探索Python类型系统typing模块示例详解
