Evi*_*inK 7 python annotations type-hinting pyi
我是 Python 注释的新手(类型提示)。我注意到文件中的许多类定义pyi
都继承了Generic[_T]
, 和_T = TypeVar('_T')
.
我很困惑,_T
这里是什么意思?
from typing import Generic, TypeVar
_T = TypeVar('_T')
class Base(Generic[_T]): pass
Run Code Online (Sandbox Code Playgroud)
Lig*_*tCC 10
我建议通读整个内置打字模块文档。
具体来说,typing.TypeVar用于指定允许多种可能的类型。如果未指定特定类型,则任何类型都是有效的。
from typing import TypeVar
T = TypeVar('T') # <-- 'T' can be any type
A = TypeVar('A', str, int) # <-- 'A' will be either str or int
Run Code Online (Sandbox Code Playgroud)
但是,如果T可以是任何类型,那么为什么要创建一个Typing.TypeVar这样的,当您可以只使用Typing.Any作为类型提示时?
原因是您可以确保特定的输入和输出参数具有相同的类型,如下例所示。
from typing import TypeVar, Dict
Key = TypeVar('Key')
Value = TypeVar('Value')
def lookup(input_dict: Dict[Key, Value], key_to_lookup: Key) -> Value:
return input_dict[key_to_loopup]
Run Code Online (Sandbox Code Playgroud)
乍一看,这似乎是一个微不足道的示例,但是这些注释要求输入字典中的键的类型与key_to_lookup
参数相同,并且输出的类型也与字典中的值的类型相匹配。
键和值作为一个整体可以是不同的类型,对于这个函数的任何特定调用,它们可以是不同的(因为键和值不限制类型),但对于给定的调用,字典的键必须匹配查找键的类型,值和返回类型相同。
如果您创建一个新的 TypeVar 并将类型限制为 float 和 int:
from typing import TypeVar, Dict
Key = TypeVar('Key')
Value = TypeVar('Value')
def lookup(input_dict: Dict[Key, Value], key_to_lookup: Key) -> Value:
return input_dict[key_to_loopup]
Run Code Online (Sandbox Code Playgroud)
该函数要求 x 和 y 要么都是浮点数,要么都是 int,并且必须返回相同的类型。如果 x 是一个浮点数而 y 是一个整数,则类型检查应该会失败。
我对这个有点粗略,但是typing.Generic(链接到官方文档)抽象基类(ABC)允许设置一个具有定义类型提示的类。他们在链接的文档中有一个很好的例子。
在这种情况下,他们正在创建一个完全通用的类型类。如果我理解正确,这允许Base[AnyName]
在代码的其他地方用作类型提示,然后可以重用AnyName
以在同一定义中的其他地方表示相同的类型。
我想这对于避免TypeVar
重复使用很有用,您基本上可以通过使用 Base 类作为类型提示来随意创建新的 TypeVar,只要您只需要它用于该本地定义的范围即可。
归档时间: |
|
查看次数: |
2416 次 |
最近记录: |