serhii.net

In the middle of the desert you can say anything you want

17 Nov 2021

211117-2327 python annotating number of elements in Tuple, Sequence, List in typing

Based on two SO answers1 2:

  • whatever: List[str,str,str] can’t be done, because lists inherently change size
  • if you know the size beforehand, use a tuple, that can be parametrized like that
  • In general, named tuples 3 are really cool in such scenarios

  1. python - How to define the size of a tuple or a list in the type hints - Stack Overflow ↩︎

  2. type hinting - Specify length of Sequence or List with Python typing module - Stack Overflow ↩︎

  3. Write Pythonic and Clean Code With namedtuple – Real Python ↩︎

Nel mezzo del deserto posso dire tutto quello che voglio.