Is there a way to do Type Hinting for List Indices in Python?
Question:
I’m really enjoying python types, but can’t seem to figure out how you might type a list with known length and indices.
Let’s say for working with bounding boxes (which contain a lot of information) we want to just store data in a list to reduce the size of the data structure… (as compared to storing in a dict)
For examples sake the structure would want to be -> List[List[x: int, y: int, w: int, h: int, frame: int]]
Is there a way to support type hinting in Python that would support hints for the indices?
Thanks!
Answers:
As per the suggestions above @Barmar mentions lists shouldn’t be used for uniform collections.
Better to use a tuple
, or better yet a dataclass
or NamedTuple
as per @Kraigolas and @Ry-
Lists are generally meant to be variable-size and homogeneous. What you’re describing (fixed length, heterogeneous) is more like a tuple, or, since the fields are labelled, a NamedTuple
or dataclass
, but, where your data is 2D, a Pandas DataFrame might make more sense. For example:
import pandas as pd
df = pd.DataFrame(
[range(1, 6)], # Dummy data just for example
columns=['x', 'y', 'w', 'h', 'frame'])
>>> df
x y w h frame
0 1 2 3 4 5
>>> df.dtypes
x int64
y int64
w int64
h int64
frame int64
dtype: object
Of course, it depends why you’re using type hinting whether this will be better. If you just want to have well-defined types for your data, this is perfect. But for static typing, this might actually be more difficult [though I haven’t tried it myself, so correct me if I’m wrong].
Under the hood, Pandas uses NumPy arrays, which can be very compact.
I’m really enjoying python types, but can’t seem to figure out how you might type a list with known length and indices.
Let’s say for working with bounding boxes (which contain a lot of information) we want to just store data in a list to reduce the size of the data structure… (as compared to storing in a dict)
For examples sake the structure would want to be -> List[List[x: int, y: int, w: int, h: int, frame: int]]
Is there a way to support type hinting in Python that would support hints for the indices?
Thanks!
As per the suggestions above @Barmar mentions lists shouldn’t be used for uniform collections.
Better to use a tuple
, or better yet a dataclass
or NamedTuple
as per @Kraigolas and @Ry-
Lists are generally meant to be variable-size and homogeneous. What you’re describing (fixed length, heterogeneous) is more like a tuple, or, since the fields are labelled, a NamedTuple
or dataclass
, but, where your data is 2D, a Pandas DataFrame might make more sense. For example:
import pandas as pd
df = pd.DataFrame(
[range(1, 6)], # Dummy data just for example
columns=['x', 'y', 'w', 'h', 'frame'])
>>> df
x y w h frame
0 1 2 3 4 5
>>> df.dtypes
x int64
y int64
w int64
h int64
frame int64
dtype: object
Of course, it depends why you’re using type hinting whether this will be better. If you just want to have well-defined types for your data, this is perfect. But for static typing, this might actually be more difficult [though I haven’t tried it myself, so correct me if I’m wrong].
Under the hood, Pandas uses NumPy arrays, which can be very compact.