Type Hints

Download python eBook

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.

Related Examples

Stats

499 Contributors: 10
Saturday, February 18, 2017
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook