数组的类型提示是什么?

Ser*_*pov 9 python arrays type-hinting

对于大多数集合,我们可以这样做:

from typing import List, Tuple, etc
Run Code Online (Sandbox Code Playgroud)

有提示吗array?如:

arr = array.array('i')
Run Code Online (Sandbox Code Playgroud)

And*_*art 8

正如 Carcigenicate 指出的,您可以array.array直接用作类型注释。但是,您不能使用array.array[int]orarray.array[float]来指定元素的类型。

如果您需要这样做,我的建议是使用MutableSequencefrom collections.abc,因为数组实现了所有必要的操作:__len____getitem____setitem____contains____iter__等。

from collections.abc import MutableSequence

arr: MutableSequence[float] = array.array('f')
Run Code Online (Sandbox Code Playgroud)