Function Annotations in Python

Function Annotations in Python

Here is the function without annotation:

>>> def olo(a, b, c):
…     return a + b + c

>>> olo(1, 2, 3)
6

We know this style. However, we can attach an annotation information in Python 3.x.x to a function object. It’s arbitrary user-defined data about a function’s arguments and result. Such annotations are optional, and their role in programming will be discovered in the future.

For now, function annotations can be used for documentation in this way:

def olo(weight: ‘in kilograms’, height: ‘in centimeters’, hair: ‘in colors’) -> str:

function_body

Function annotations can be used for pre-condition checking in our code. They don’t perform any action, but allow for a elegant way to describe the expected behavior of elements of our function.

Let’s see more programming example:

def olo(x: ‘j’, y: list, z: 1 + 2 + 3)  -> max(4, 34):

function_body

There are two types of annotations here:

— after : it’s the information for a function argument

— after -> it’s the function’s return value

We’ll use the following to see how it works:

func_name.__annotations__

Let’s see that with the function olo():

>>> olo.__annotations__
{‘z’: 6, ‘y’: <class ‘list’>, ‘x’: ‘j’, ‘return’: 34}
>>>

Our result is the dictionary with keys and values, and all the expressions has been evaluated.

So function annotations are coded in the function header as arbitrary expressions associated with arguments and return values. For arguments, they appear after a
colon following the argument’s name; for return values, they are written
after a -> following the arguments list. It’s clear.

Let’s see the real function with annotations:

def olo(a:(12, 120), c:bool, b:’Tolek’) -> int:

return a*b-c

We see the result:

>>> olo(1,2,3)
1
>>>

Let’s see the annotations in this function:

>>> olo.__annotations__
{‘c’: <class ‘bool’>, ‘b’: ‘Tolek’, ‘a’: (12, 120), ‘return’: <class ‘float’>}
>>>

So we can call such a function as usual. Python collects the annotations in a dictionary and attaches to the function object.

Our function header can be somewhat cluttered because of the annotations. However, we can still work with functions like before. Let’s look at the example:

def olo(a:(12, 120) = 10, c:bool = 20, b:’Tolek’ = 30) -> int:

return a*b-c

The calling normal values:

>>> olo(1,2,3)
1

The calling with default values:
>>> olo()
280

The calling with keyword values:
>>> olo(1,2,b=40)
38

browsing our annotations:
>>> olo.__annotations__
{‘c’: <class ‘bool’>, ‘b’: ‘Tolek’, ‘a’: (12, 120), ‘return’: <class ‘int’>}
>>>

Above annotations aren’t too good for a documentation purposes, because they only complicated the code, not giving us anything. I wished to show you how it looks, because it’s a new feature of Python. Now, let’s see how function annotations should look to help us:

def olo(a:int, b:int, c:int) -> int:

return a+b+c

So we know that we need three int arguments, and the return value will be int too.

>>> olo(1,2,3)
6
>>>

 

Leave a comment