Python Language Type Hints

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9

Syntax

  • typing.Callable[[int, str], None] -> def func(a: int, b: str) -> None
  • typing.Mapping[str, int] -> {"a": 1, "b": 2, "c": 3}
  • typing.List[int] -> [1, 2, 3]
  • typing.Set[int] -> {1, 2, 3}
  • typing.Optional[int] -> None or int
  • typing.Sequence[int] -> [1, 2, 3] or (1, 2, 3)
  • typing.Any -> Any type
  • typing.Union[int, str] -> 1 or "1"
  • T = typing.TypeVar('T') -> Generic type

Remarks

Type Hinting, as specified in PEP 484, is a formalized solution to statically indicate the type of a value for Python Code. By appearing alongside the typing module, type-hints offer Python users the capability to annotate their code thereby assisting type checkers while, indirectly, documenting their code with more information.



Got any Python Language Question?