As mentioned in previous answers, you use typing.Tuple to specify a fixed number of elements.
Is there a shortcut for long lists? What if I want to set it to 10 floats?
List[float * 10] # This doesn't work.
Instead of explicitly passing a list of N element types (provided that you need all the same element types), you can use the multiplication operator in the tuple (does not work with the list) to repeat an element of type N times.
eg
>>> print(Tuple[(float,)*4]) typing.Tuple[float, float, float, float]
To use as a hint type:
def foo(bar: Tuple[(float,)*4]): pass
Help conclusion:
>>> help(foo) Help on function foo in module __main__: foo(bar: Tuple[float, float, float, float])
Although for clarity, I prefer to be explicit for shorter sequences, that is, using Tuple[float, float, float, float]
user2856
source share