typing模块的作用
自python3.5开始,pep484为python引入了类型注解(type hints)
类型检查,防止运行时出现参数和返回值类型、变量类型不符合。
作为开发文档附加说明,方便使用者调用时传入和返回参数类型。
该模块加入后并不会影响程序的运行,不会报正式的错误,只有提醒pycharm目前支持typing检查,参数类型错误会黄色提示
常用类型
int,long,float: 整型,长整形,浮点型
bool,str: 布尔型,字符串类型
list, tuple, dict, set:列表,元组,字典, 集合
iterable,iterator:可迭代类型,迭代器类型
generator:生成器类型
基本类型指定
示例
def test(a:int, b:str) -> str:
print(a, b)
return 1000
if __name__ == '__main__':
test('test', 'abc')
函数test,a:int 指定了输入参数a为int类型,b:str b为str类型,-> str 返回值为srt类型。
可以看到,在方法中,我们最终返回了一个int,此时pycharm就会有警告;
当我们在调用这个方法时,参数a我们输入的是字符串,此时也会有警告;
但非常重要的一点是,pycharm只是提出了警告,但实际上运行是不会报错,毕竟python的本质还是动态语言
复杂的类型标注
示例1
from typing import list
vector = list[float]
def scale(scalar: float, vector: vector) -> vector:
return [scalar * num for num in vector]
# typechecks; a list of floats qualifies as a vector.
new_vector = scale(2.0, [1.0, -4.2, 5.4])
示例2
from typing import dict, tuple, sequence
connectionoptions = dict[str, str]
address = tuple[str, int]
server = tuple[address, connectionoptions]
def broadcast_message(message: str, servers: sequence[server]) -> none:
...
# the static type checker will treat the previous type signature as
# being exactly equivalent to this one.
def broadcast_message(
message: str,
servers: sequence[tuple[tuple[str, int], dict[str, str]]]) -> none:
...
):
...
这里需要注意,元组这个类型是比较特殊的,因为它是不可变的。
所以,当我们指定tuple[str, str]时,就只能传入长度为2,并且元组中的所有元素都是str类型
泛型指定
示例
from typing import sequence, typevar, union
t = typevar('t') # declare type variable
def first(l: sequence[t]) -> t: # generic function
return l[0]
t = typevar('t') # can be anything
a = typevar('a', str, bytes) # must be str or bytes
a = union[str, none] # must be str or none
创建变量时的类型指定
from typing import namedtuple
class employee(namedtuple):
name: str
id: int = 3
employee = employee('guido')
assert employee.id == 3
不足之处
示例
from typing import list
def test(b: list[int]) -> str:
print(b)
return 'test'
if __name__ == '__main__':
test([1, 'a'])
从这个例子可以看出来,虽然我们指定了list[int]即由int组成的列表,但是,实际中,只要这个列表中存在nt(其他的可以为任何类型),就不会出现警告
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!