Title: Function Annotation Examples
Slug: function_annotations_examples
Summary: Function Annotation Examples in Python.
Date: 2016-01-23 12:00
Category: Python
Tags: Basics
Authors: Chris Albon

Interesting in learning more? Check out Fluent Python

Create A Function With Annotations


In [1]:
''' 
Create a function. 

The argument 'text' is the string to print with the default value 'default string'
and the argument 

The argument 'n' is an integer of times to print with the default value of 1. 

The function should return a string.
'''
def print_text(text:'string to print'='default string', n:'integer, times to print'=1) -> str:
    return text * n

Run The Function


In [2]:
# Run the function with arguments
print_text('string', 4)


Out[2]:
'stringstringstringstring'

In [3]:
# Run the function with default arguments
print_text()


Out[3]:
'default string'