Chapter 7. Function Annotations

Functions and methods can be defined with annotations—expressions that can be used in a function’s signature. Here’s the general syntax:

def functionName(par1 : exp1par2 : exp2, ..., parN : expN)-> rexp:    suite

Every colon expression part (: expX) is an optional annotation, and so is the arrow return expression part (-> rexp). The last (or only) positional parameter (if present) can be of the form *args, with or without an annotation; similarly, the last (or only) keyword parameter (if present) can be of the form **kwargs, again with or without an annotation.

If annotations are present they are added to the function’s __annotations__ dictionary; if they are not present this dictionary is empty. The ...

Get Advanced Python 3 Programming Techniques now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.