Python类型注解与typing的使用

2022-09-29 13:37:52

来源:https://liaolei.cc/?p=99

众所周知, Python是一种动态语言 在声明一个变量时我们不需要显示的声明它的类型.

类型注释可以提高代码的可读性和易用性, 帮助开发者写出更加严谨的代码, 让调用者减少类型方面的错误, 但是, 类型注解语法传入的类型表述有限, 不能说明复杂的类型组成情况, 因此引入了typing模块, 用来实现复杂的类型表述

一、类型注解

在声明变量类型时,变量后方紧跟一个冒号,冒号后面跟一个空格,再跟上变量的类型.
在声明方法返回值的时候,箭头左边是方法定义,箭头右边是返回值的类型,箭头左右两边都要留有空格.

a:int=2defadd(a:int)->int:pass

二、typing

类型检查,防止运行时出现参数、返回值类型不符。
作为开发文档附加说明,方便使用者调用时传入和返回参数类型。
模块加入不会影响程序的运行不会报正式的错误,pycharm支持typing检查错误时会出现黄色警告。
1.基础用法

from typingimport List, Tuple, Dict

names: List[str]=["li","tom"]
version: Tuple[int,int,int]=(6,6,6)
operations: Dict[str,bool]={'sad':False,'happy':True}

2.类型

TypeDescription
int整型
float浮点数字
bool布尔
str字符串
bytes8位字符
object任意对象
List(str)字符串组成的列表
Tuple[int, …]任意数量的int对象的元组
Tuple[int, int]两个int对象的元组
Dict[str, int]键是 str 值是 int 的字典
Iterable[int]包含 int 的可迭代对象
Sequence[bool]布尔值序列(只读)
Mapping[str, int]从 str 键到 int 值的映射(只读)
Any具有任意类型的动态类型值
Union联合类型
Optional参数可以为空或已经声明的类型
Mapping映射,是 collections.abc.Mapping 的泛型
MutableMappingMapping 对象的子类,可变
Generator生成器类型, Generator[YieldType、SendType、ReturnType]
NoReturn函数没有返回结果
Set集合 set 的泛型, 推荐用于注解返回类型
AbstractSetcollections.abc.Set 的泛型,推荐用于注解参数
Sequenceollections.abc.Sequence 的泛型,list、tuple 等的泛化类型
TypeVar自定义兼容特定类型的变量
NewType声明一些具有特殊含义的类型
Callable可调用类型, Callable[[参数类型], 返回类型]

List

var: List[intorfloat]=[2,3.5]
var: List[List[int]]=[[1,2],[2,3]]

Tuple

person: Tuple[str,int,float]=('Mike',22,1.75)

Dict、Mapping、MutableMapping
Dict、字典,是 dict 的泛型;Mapping,映射,是 collections.abc.Mapping 的泛型。

Dict 推荐用于注解返回类型
Mapping 推荐用于注解参数
MutableMapping 则是 Mapping 对象的子类,在很多库中也经常用 MutableMapping 来代替 Mapping。

defsize(rect: Mapping[str,int])-> Dict[str,int]:return{'width': rect['width']+100,'height': rect['width']+100}

Set、AbstractSet
Set 推荐用于注解返回类型
AbstractSet 用于注解参数

defdescribe(s: AbstractSet[int])-> Set[int]:returnset(s)
 sequenceDiagram```

在某些情况下,我们可能并不需要严格区分一个变量或参数到底是列表 list 类型还是元组 tuple 类型,我们可以使用一个更为泛化的类型,叫做 Sequence,其用法类似于 List
```py
def square(elements: Sequence[float]) -> List[float]:
    return [x ** 2 for x in elements]

NoReturn
当一个方法没有返回结果时,为了注解它的返回类型,我们可以将其注解为 NoReturn

defhello()-> NoReturn:print('hello')

Any
一种特殊的类型,它可以代表所有类型,静态类型检查器的所有类型都与 Any 类型兼容,所有的无参数类型注解和返回类型注解的都会默认使用 Any 类型

defadd(a: Any)-> Any:return a+1

TypeVar
自定义兼容特定类型的变量

height=1.75
Height= TypeVar('Height',int,float,None)defget_height()-> Height:return height

NewType
声明一些具有特殊含义的类型

Person= NewType('Person', Tuple[str,int,float])
person= Person(('Mike',22,1.75))

Callable
可调用类型,它通常用来注解一个方法

defdate(year:int, month:int, day:int)->str:returnf'{year}-{month}-{day}'

参数类型和返回值类型都要注解出来

defget_date_fn()-> Callable[[int,int,int],str]:return date

lambda类型标注

is_even: Callable[[int],bool]=lambda x:(x%2==0)

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]
defprocess(: Union[str, Callable]):isinstance(,str):# str2fn and processpassisinstance(, Callable):()

Optional
参数可以为空或已经声明的类型, 即 Optional[X] 等价于 Union[X, None]。

不等价于可选参数,当它作为参数类型注解的时候,不代表这个参数可以不传递了,而是说这个参数可以传为 None

# 当一个方法执行结果, 如果执行完毕就不返回错误信息, 如果发生问题就返回错误信息from typingimport Optionaldefjudge(result:bool)-> Optional[str]:if result:return'Error Occurred'

Generator

生成器类型, 声明方式, 其后的中括号紧跟着三个参数,分别代表 YieldType、SendType、ReturnType

  • YieldType: yield 关键字后面紧跟的变量的类型
  • SendType: yield 返回的结果的类型就是 SendType, 可为None
  • ReturnType: 最后生成器 return 的内容类型, 可为None
defecho_round()-> Generator[int,float,str]:
    sent=yield0while sent>=0:
        sent=yieldround(sent)return'Done'

三、实例

代码来源于 requests-html库

下面是一些使用示例:

# Typing.
_Find= Union[List['Element'],'Element']
_XPath= Union[List[str], List['Element'],str,'Element']
_Result= Union[List['Result'],'Result']
_HTML= Union[str,bytes]
_BaseHTML=str
_UserAgent=str
_DefaultEncoding=str
_URL=str
_RawHTML=bytes
_Encoding=str
_LXML= HtmlElement
_Text=str
_Search= Result
_Containing= Union[str, List[str]]
_Links= Set[str]
_Attrs= MutableMapping
_Next= Union['HTML', List[str]]
_NextSymbol= List[str]
  • 作者:呆萌的代Ma
  • 原文链接:https://blog.csdn.net/weixin_35757704/article/details/121703932
    更新时间:2022-09-29 13:37:52