Functions Anotations in Python 3

Presenter Notes

How about this?

def add(x: int, y: int) -> int:
    return x+y

This is valid Python 3 code!

Presenter Notes

Function metadata

def square(x):
    """Computes square of a number."""
    return x*x

>>> square.__name__
'square'
>>> square.__module__
'__main__'
>>> square.__doc__
'Computes square of a number.'

Presenter Notes

Annotations are function metadata

Presenter Notes

What is mypy?

  • It is a static type checker for Python
  • Written by Jukka for his Ph.D. thesis.

Presenter Notes

Questions?

Anand Chitipothu http://anandology.com/ @anandology

Slides: http://anandology.com/presentations/python3-function-anotations

Presenter Notes