描述
在 Python 3.5 或以上的版本中,支持类型提示(更多信息请参见此处)。然而,常用类型的正确用法并没有得到很好的记录。
例如,从官方网站上,我能够获得以下(适当的)用法:
字符串
# The function signature of greeting is: Callable[[str], str]
def greeting(name: str) -> str:
return 'Hello ' + name
整数
# The function signature of add is: Callable[[int, int], int]
def add(a: int, b: int) -> int:
return a + b
浮点数
# The default value of x is 1.0
def reciprocal(x: float = 1.0) -> float:
from math import nan
if x == 0:
return nan
return 1 / x
列表
from typing import List, TypeVar
T = TypeVar('T')
def repeat(x: T, n: int) -> List[T]:
return [x] * n
元组
from typing import Tuple, TypeVar
T = TypeVar('T')
def double(x: T) -> Tuple[T, T]:
return (x, x)
问题
我的问题是:
1. map
的返回类型是什么?
from typing import Iterable
# Is this correct?
x: Iterable[float] = map(int.__float__, [1, 2, 3])
我不确定上面的x
是正确的类型提示。
2. 从更广泛的意义上讲,map
的“函数签名”是什么?
from typing import Callable, Iterable, TypeVar
T = TypeVar('T')
U = TypeVar('U')
# In the above usage, the type of the map function seems to be:
Function1 = Callable[[T], U]
typeOfMap = Callable[[Function1, Iterable[T]], Iterable[U]]
# Or in one line:
typeOfMap = Callable[[Callable[[T], U], Iterable[T]], Iterable[U]]
但实际上,map函数可以接受多个可迭代对象。这在文档中有说明:
map(function, iterable, ...)
使用时可以这样:
# The result is: [['A'], ['B', 'B'], ['C', 'C', 'C']]
result = list(map(repeat, ['A', 'B', 'C'], [1, 2, 3]))
T1 = TypeVar('T1')
T2 = TypeVar('T2')
R = TypeVar('R')
# So, the type of map function in this usage seems to be:
Function2 = Callable[[T1, T2], R]
typeOfMap = Callable[[Function2, Iterable[T1], Iterable[T2]], Iterable[R]]
一般来说,我猜大概是下面这样,但这并不是正确的写法:
FunctionN = Callable[[T1, T2, ..., Tn], R]
typeOfMap = Callable[[FunctionN, Iterable[T1], Iterable[T2], ..., Iterable[Tn]], Iterable[R]]
那么,正确的写法是什么?
3.通常情况下,我在哪里可以找到Python函数/方法的正确类型提示,包括内置的以及核心库中的函数/方法?
我主要需要它们来学习。
4.有没有什么方法可以输出由编译器/解释器计算得出的类型推断结果,就像Haskell或Erlang一样?
我知道Haskell和Erlang是函数式编程语言,变量是不可变的,所以这将更容易实现,但如果Python也具有类似的功能,我想了解一下。
5.有没有办法检查我的类型提示是否正确?
或者至少让它在编译时/运行时显示一些警告/错误,以便我知道有问题。
参考资料
在撰写本文时,最新的稳定版本是3.8.0。
mypy
。我会去看一下的。对于那些想要使用mypy
的人,这里是指向它的链接。 - Siu Ching Pong -Asuka Kenji-