Function Annotations of Python 3

Python 3 features functions annotation that simply let every users or coder add random metadata to any functions and its return value. This one is a part of main Python 3 feature. Here I will take in a tour of – “how to take full advantages of general purpose functions and the annotations and combine them with decorators”. It’s not a tough as it is looking. Once you go through this tutorial, you will get a clear idea about this one.

What we cover in this Python Function annotation tutorial-

  • How to use the annotations
  • When to use the annotations

Let’s get a deep idea about function annotations-

All the details of functions annotations are already specified in PEP-3107. Do you know what the objective of this is? It’s to give a standard way to associate metadata to all function arguments and the return value of those functions. Many Python experts got cases where they used different methods like custom decorators and some custom doc string format and sometimes they adds some custom features to the object of the function. But always remember that Python does not bless any annotation from any corner. So the question may come to your mind- what does it do then? It purely supports the associating metadata and moreover it provides a syntactic support to get easy access to it. So notations are optional, and you can use it wherever you want (with the precision of course).

Python 3 Function Annotation- Example

Here is an example. A function foo() which takes three arbitrary arguments named a, b and c and prints their summation. Note one thing- foo() returns nothing in this case. The very first argument which is named “an” is not annotated. The second argument which is named “b” is annotated with the string ‘annotating b’, and the third argument which is named c is annotated with type int. The return value is also annotated with the type “float”. Note one thing that “->” syntax for annotating the return value.

def foo(a, b: ‘annotating b’, c: int) -> float:

print(a + b + c)

These annotations will have no impact whatsoever on the execution of these functions over the time. Let’s code foo() twice, one with “int” arguments and another with string arguments. In both cases, foo() does the right options, and all the annotations of coding are ignored.

1

2

3

4

5

foo(‘Hello’, ‘, ‘, ‘World!’)

Hello, World!

 

foo(1, 2, 3)

6

Accessing Function Annotations

The function object has some important attributes; one of them is called ‘annotations.’ It is a general mapping that does nothing but maps each argument detailing to its respective annotation. The return value of the annotation is mapped to the key named ‘return’, which does not come in conflict with any argument because ‘return’ is a reserved word that can’t be there as an argument name.

1

2

3

4

5

6

def bar(*args, **kwargs: ‘the keyword arguments dict’):

print(kwargs[‘return’])

 

d = {‘return’: 4}

bar(**d)

4

Share your opinions and of course the experience of accessing the functions. Share your thoughts in the comment section below.

One Response

  1. Ron Barak July 30, 2016