0

Consider this example dataclass which contains a variable x which is a list of 5 integers. What is the correct way to write the type hint? Individually specifying all 5 int values in the list seems cumbersome? What if my list was N elements long and N was very big?

@dataclass
class Example:
    x: list[int, int, int, int, int] = [0, 0, 0, 0, 0]

Can I just write:

@dataclass
class Example:
    x: list[int] = [0, 0, 0, 0, 0]

Or is there another way I can write the type hint to indicate to the user that this list is always 5 elements long?

Mike
  • 143
  • 9
  • 1
    Have a look at [Specify length of Sequence or List with Python typing module](https://stackoverflow.com/q/44833822/5168011) – Guy Jul 06 '22 at 13:08
  • Thanks - that has answered my question – Mike Jul 06 '22 at 13:15

0 Answers0