我试图在Python中指定itertool.count
对象的类型,如下所示:
from itertools import count
c: count = count()
但是,运行mypy
会产生以下错误:
test.py:3: error: Function "itertools.count" is not valid as a type
test.py:3: note: Perhaps you need "Callable[...]" or a callback protocol?
Found 1 error in 1 file (checked 1 source file)
这似乎是因为itertools.count
的行为类似于函数。但是,它返回一个itertools.count
对象,如下所示
In [1]: import itertools
In [2]: type(itertools.count()) is itertools.count
Out[2]: True
那么,我应该如何指定count()
结果的类型呢
在
itertools.pyi
中有以下注释:因此,在代码中,您可以这样做:
相关问题 更多 >
编程相关推荐