typing库

本文介绍了Python的typing库,该库用于在Python中进行类型标注,以提高代码的可读性和工具的兼容性。文中详细讲解了如何使用类型别名、NewType以及泛型等概念,帮助开发者更好地规范函数参数和返回值的类型。
摘要由CSDN通过智能技术生成

typing 库

引入

在日常代码编写中,由于python语言特性,不用像go等编译性语言一样,在定义函数时就规范参数和放回值的类型。

def demo(a, b):
    return "a+b"  
'''
	此时 a 和 b 可以传入任意类型参数
	毫无疑问,这一特性,在定义函数阶段是非常方便的,
毕竟能少写好多东西。
	但是,在别人调用你写的函数,或者你调用别人写的函数时,
就不那么友好了。
	因此python推出了 注释功能
'''
def Demo(a: int, b: int) -> int:
    return a + b

在这里插入图片描述

但是,有时候函数接受的参数是列表,里面数据需要全是float类型,这个时候该咋办呢?

因此官方推出了typing库,typing 库是python 提供用来类型标注支持的工具库。可以用来规范开发过程中的规范,可被用于第三方工具,比如类型检查器、集成开发环境、静态检查器等。Python 在运行时并不强制标注函数和变量类型。

备注: typing库于3.5版本引入,本文基于3.8 解释器版本作于记录,在3.10之后 typing包功能更加强大。

typing包最基本的支持由 AnyUnionTupleCallableTypeVarGeneric 类型组成。

类型别名

类型别名通过将类型分配给别名来定义

from typing import List

Vector = List[float]  # Vector 和 List[float] 将被视为可互换的同义词

# Vector 类型数据是 列表里面放float类型数据 同 go 中 float类型切片
def scale(scalar: float, vector: Vector) -> Vector:
    return [scalar * num for num in vector]

print(scale.__annotations__)  # 查看函数定义的入参和出参类型
# {'scalar': <class 'float'>, 'vector': typing.List[float], 'return': typing.List[float]}
new_vector = scale(2.0, [1.0, -4.2, 5.4])
print(new_vector)
# [2.0, -8.4, 10.8]

类型别名可用于简化复杂类型签名。例如:

from typing import Dict, Tuple, Sequence

ConnectionOptions = Dict[str, str]
Address = Tuple[str, int]
Server = Tuple[Address, ConnectionOptions]

NewType

可以通过NewType() 辅助函数创建不同的类型

from typing import NewType

UserId = NewType('UserId', int)
some_id = UserId(524313)

def get_user_name(user_id: UserId) -> str:
    return "%s" % user_id


user_a = get_user_name(some_id)
# 静态类型检查器会将新类型视为它是原始类型的子类。
user_b = get_user_name(22)  

def user_name(id: int) -> str:
    return "%s" % id

user_name(some_id)

print(type(some_id))  # <class 'int'>

print(524313 is some_id)  # True

在这里插入图片描述

UserId 类型实际上还是 int 类型, 能够支持 int 所有操作.这些检查只是由静态类型检查器来执行,在程序运行时,UserId = NewType('UserId', int) 会产生一个 UserId 函数该函数会立即放回你传递给它的任何参数,不会产生一个新的类,也不会引入超出常规函数调用的额外开销。

# NewType 源码
def NewType(name, tp):
    def new_type(x):
        return x
    new_type.__name__ = name
    new_type.__supertype__ = tp
    return new_type

这同样也意味着,UserId 无法产生子类型。因为实际上并没有这个类

from typing import NewType

UserId = NewType('UserId', int)
class AdminUserId(UserId):   # 执行抛异常
    pass

# TypeError: function() argument 1 must be code, not str

然而,我们可以在 “派生的” NewType 的基础上创建一个 NewType

from typing import NewType

UserId = NewType('UserId', int)

ProUserId = NewType('ProUserId', UserId)

Callable

期望特定签名的回调函数的框架可以将类型标注为 Callable[[Arg1Type, Arg2Type], ReturnType]

Callable 用来检查传入的参数是否是个可调用对象,

[Arg1Type, Arg2Type] :入参

ReturnType : 出参

def feeder(get_next_item: Callable[[], str]) -> None:
    # Body
    return 

# 等价写法,其实就是将函数作为参数传入, 写装饰器的时候可以用用
def	feeder_test(func) -> Callable[[], str]: 
    return func


Sequence

typing 库中的 Sequence 是 collections.abc.Sequence 的泛型版本

collections.abc.Sequence 只读且可变的序列 sequences 的抽象基类。

一种 iterable,它支持通过 __getitem__() 特殊方法来使用整数索引进行高效的元素访问,并定义了一个返回序列长度的 __len__() 方法。内置的序列类型有 liststrtuplebytes。注意虽然 dict 也支持 __getitem__()__len__(),但它被认为属于映射而非序列,因为它查找时使用任意的 immutable 键而非整数。

collections.abc.Sequence 抽象基类定义了一个更丰富的接口,它在 __getitem__()__len__() 之外又添加了 count(), index(), __contains__()__reversed__()。 实现此扩展接口的类型可以使用 register() 来显式地注册。

泛型(Generic)

泛型,是程序设计语言的一种风格或范式。泛型允许程序员在强类型程序设计语言中编写代码时使用一些以后才指定的类型,在实例化时作为参数指明这些类型。各种程序设计语言和其编译器、运行环境对泛型的支持均不一样。

Python作为一个动态语言,并不会做类型检查,所谓的“类型声明”只是一个“提示(hints)”或者“注解(annotations)”的作用。同理,Python中所谓的泛型也并不会想静态语言那样对输入的参数进行严格的要求,但是可以对类型进行“提示”和“限制”,减少不必要的类型错误。

泛型函数

from typing import TypeVar, List, Union

# 通过TypeVar限定为整数型的列表和浮点数的列表
T = TypeVar("T", bound=Union[List[int], List[float]])
# 也可以写成如下形式
T = TypeVar("T", List[int], List[float])

def printList(l: T):  # printList(l: Sequence[T]):
    for e in l:
        print(e)

printList([1, 2, 3])           # 打印整数型列表
printList([1.1, 2.2, 3.3])     # 打印浮点数列表
printList(["a", "b", "c"])     # 字符串型列表,出现异常标记

泛型类

from typing import TypeVar, Union, Generic, List

# 接受所有类型
T = TypeVar("T")
# 通过TypeVar限定为整数型的列表和浮点数的列表
T = TypeVar("T", bound=Union[int, float])


class MyList(Generic[T]):

    def __init__(self, size: int) -> None:
        self.size = size
        self.list: List[T] = []

    def append(self, e: T):
        self.list.append(e)
        print(self.list)


# 适用于整数型
intList = MyList[int](3)  # 通过[int]进行类型提示!
intList.append(101)

# 也适用于浮点数
floatList = MyList[float](3)
floatList.append(1.1)

# 但不适用于字符串,以下代码通过mypy检查会报错!
strList = MyList[str](3)
strList.append("test")

Generic[T] 作为基类定义了类 LoggedVar 采用单个类型参数 T。这也使得 T 作为类体内的一个类型有效。

Generic 基类定义了 __class_getitem__() ,使得 MyList[t] 作为类型有效:

Generic 每个参数的类型变量必须是不同的。这是无效的:

from typing import TypeVar, Generic
...

T = TypeVar('T')

class Pair(Generic[T, T]):   # 无效
    ...

Generic 支持多重继承:

from typing import TypeVar, Generic, Sized

T = TypeVar('T')

class LinkedList(Sized, Generic[T]):
    ...
    
# Sized  提供了 __len__() 方法的抽象基类。 
# 还有很多 https://docs.python.org/zh-cn/3.8/library/collections.abc.html#

其他参考

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

go&Python

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值