考虑到 typing
的 Python 文档为什么下面的代码不起作用?
>>> Vector = list[float]
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: 'type' object is not subscriptable
在文档中有与我上面提到的相同的示例。 hereVector = list[float]
def scale(scalar: float, vector: Vector) -> Vector:
return [scalar * num for num in vector]
我没有发现有关此示例的问题。
最佳答案
使用 []
的能力类型的运算符,如 list
类型提示是在 3.9 中添加的。
https://docs.python.org/3/whatsnew/3.9.html#type-hinting-generics-in-standard-collections
在早期版本中它会产生你描述的错误,你需要导入 List
来自 typing
的对象反而。
from typing import List
List[float]
关于python - 键入库中的示例代码导致 TypeError : 'type' object is not subscriptable, 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66154004/