Is the List in python homogeneous or heterogeneous?
One more argument for homogeneity of lists: the PEP 484 and the new typing
module. The items of tuple
can be individually typed with Tuple
:
Tuple
, used by listing the element types, for exampleTuple[int, int, str]
. The empty tuple can be typed asTuple[()]
. Arbitrary-length homogeneous tuples can be expressed using one type and ellipsis, for exampleTuple[int, ...]
. (The ... here are part of the syntax, a literal ellipsis.) [reference]
>>> import typing
>>> typing.Tuple[str, int, float]
typing.Tuple[str, int, float]
However a list
, via typing.List
only accepts a single type parameter:
List
, used asList[element_type]
>>> typing.List[str, int, float]
Traceback (most recent call last):
...
TypeError: Too many parameters for typing.List<~T>; actual 3, expected 1