Skip to Content
Advanced Python 3 Programming Techniques
book

Advanced Python 3 Programming Techniques

by Mark Summerfield
February 2009
Intermediate to advanced
68 pages
1h 51m
English
Addison-Wesley Professional
Content preview from Advanced Python 3 Programming Techniques

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python Network Programming Techniques

Python Network Programming Techniques

Marcel Neidinger
Scientific Computing with Python 3

Scientific Computing with Python 3

Claus Führer, Claus Fuhrer, Jan Erik Solem, Olivier Verdier

Publisher Resources

ISBN: 9780321637727