Specify the length of a sequence or list using the Python text input module

I give away the Python module typing shot.

I know this is true to indicate the length of the List as follows:

 List[float, float, float] # List of 3 floats <-- NOTE: this is not valid Python 

Is there a shorthand for longer lists? What if I want to set 10 floats?

 List[float * 10] # This doesn't work. 

Any idea, if possible, would be convenient.


* NOTE: It turns out that providing multiple arguments to Sequence[] (and its subclasses) this way is NOT currently valid Python. In addition, it is currently not possible to specify the length of a Sequence using the typing module in this way.

+7
python type-hinting mypy
source share
3 answers

You can not. A list is a variable structure of variable length. If you need a fixed-length structure, use a tuple instead:

 Tuple[float, float, float, float, float, float, float, float, float, float] 

Or even better, use named tuple , which has both indexes and named attributes:

 class BunchOfFloats(NamedTuple): foo: float bar: float baz: float spam: float ham: float eggs: float monty: float python: float idle: float cleese: float 

A list is simply the wrong data type for a fixed-length data structure.

+3
source share

So far, only tuples support the indication of a fixed number of fields, and it does not have shortcuts for a fixed number of repetitions.

Here is the definition and documentation line from the typing module:

 class Tuple(tuple, extra=tuple, metaclass=TupleMeta): """Tuple type; Tuple[X, Y] is the cross-product type of X and Y. Example: Tuple[T1, T2] is a tuple of two elements corresponding to type variables T1 and T2. Tuple[int, float, str] is a tuple of an int, a float and a string. To specify a variable-length tuple of homogeneous type, use Tuple[T, ...]. """ __slots__ = () def __new__(cls, *args, **kwds): if _geqv(cls, Tuple): raise TypeError("Type Tuple cannot be instantiated; " "use tuple() instead") return _generic_new(tuple, cls, *args, **kwds) 

Since lists are variable type variable lengths, it makes no sense to use a type declaration to indicate a fixed size.

+2
source share

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]

0
source share

All Articles