Type Hints in Python

Learned about this today, so I'm organizing it!

Reference: python docsarrow-up-right

Before getting started

  • The Python runtime does not enforce type annotations on functions or variables

    • You need to use an IDE or type checker to verify them

  • You can provide type hints to indicate types when using methods

Function Annotations

docsarrow-up-right

  • Function annotations can be used for Parameters and return values, but they are not mandatory!

  • Function annotations can be used to provide type checking

    • However, Lambda does not support annotations!

Syntax

1. Parameters

ex1) parameter

ex2) excess parameter ( *args, **kwargs )

2. Return Values

ex)

+

Type aliases

: Additionally, you can also use aliases!

ex)

Last updated