Book Image

Python Essentials

By : Steven F. Lott
Book Image

Python Essentials

By: Steven F. Lott

Overview of this book

Table of Contents (22 chapters)
Python Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Writing additional function annotations


The Python Enhancement Proposal (PEP) number 3107 specifies additional annotations which can be applied to a function definition. Additionally, PEPs 482, 483, and 484 cover some related ideas.

This is important only because Python has some optional syntax that we may see. In Python 3.5, there may be additional tools for the type of information provided in this optional syntax. The annotated code can look like this:

def roller( n: int, d: int = 6 ) -> tuple:
    return tuple(random.randint(1,d) for _ in range(n))

This function includes additional : expression annotations after each parameter. It also includes a -> expression annotation to show the return type of the function. All of the annotation expressions in this example are the names of built-in types.

In order to describe more complex structures, an additional typing module can offer the tools for defining a more exact Tuple[int, …] as the return type for this function. This is an exciting development...