Lists in Python

前端 未结 7 2124
太阳男子
太阳男子 2021-01-07 05:20

Is the List in python homogeneous or heterogeneous?

7条回答
  •  时光说笑
    2021-01-07 05:59

    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 example Tuple[int, int, str]. The empty tuple can be typed as Tuple[()]. Arbitrary-length homogeneous tuples can be expressed using one type and ellipsis, for example Tuple[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 as List[element_type]

    >>> typing.List[str, int, float]
    Traceback (most recent call last):
      ...
    TypeError: Too many parameters for typing.List<~T>; actual 3, expected 1
    

提交回复
热议问题