Annotations for Python Functions
Annotations on functions can be written as arbitrary expressions and evaluated alongside the functions at compile time. At runtime, they do not exist, and these expressions have no significance in Python. A third party application or external python libraries utilizes and interprets them.
We all know that third-party libraries make use of function annotations due to the benefits that these annotations provide. For
example:
-
Function annotations with strings
can be used as messages at compile time to describe the functionality of different methods, classes and variables. -
[def fxn(a:”int”, b:”float”=5.0) -> “int”]
Since python provides dynamic type checking, and since there is no method to check the return type or the data type, these annotations can be used to declare the data types of the parameters in the aforementioned example. These are used by mypy and other libraries.
annotations.
Syntax for the function annotations
Many different forms of function annotations are possible, including: 1. Annotating functions with only a few parameters:
-
def
functionName(argumentName : expression):
After the name of the argument, we can use a colon, and then write the expression following the colon. Any valid argument data type or message string can be used in the expression. Case in point:
-
def
fxn(var1 : expression1,var2:expression2:
96
):
2. Annotations for functions that take in too many parameters:
This method of annotating functions allows us to specify an arbitrary number of arguments with the same expression as function parameters. Case in point:
-
def
fxn(*arg1: expression1,**arg2:expression2):
3. Parameter nesting function annotation:
Function annotations must be applied to each individual element of the list if it is to be used as an argument in a function call. Case in point:
-
def
fxn((var1:expression,var2:expression),(var3:expression,var4:expression)):
4. Return type annotations for functions:
The ‘->’ operator is used to annotate the return type. Case in point:
-
def
fxn(var1:expression) -> expression2:
The following explains how to make sense of Python’s function annotations:
example:
Example:
Resulting
-
# program to print Fibonacci series using function annotations
-
def
fibonacci(n:
‘int’
, res:
‘list’
=[])->
‘list’
:
-
if
n ==
0
:
-
return
res
-
else
:
-
if
len(res)<
2
:
-
res.append(
1
)
-
fibonacci(n-
1
, res)
-
else
:
-
last = res[-
1
]
-
second_last = res[-
2
]
-
res.append(last + second_last)
-
fibonacci(n-
1
, res)
-
return
res
-
print
(fibonacci(
8
))
Clarification:
The function for determining the initial n Fibonacci numbers is presented above in bold. The function expects n and a blank list as input and produces the corresponding list as output.
We’ve specified that n is an integer and that res is a list in the function’s documentation. The return type was also specified via function annotation.
list.
Note: we can access all the function annotations we used in the function by various methods:
1. “__annotations__” in action:
Annotations within a function can be retrieved using the __annotations__ attribute. It gives back the dictionary with the key being the parameters and the value being the actual values of those arguments.
expression.
Example 2:
Resulting
-
# Python program to print Fibonacci series using function annotations and to access the annotations
-
def
fibonacci(n:
‘int’
, res:
‘list’
=[])->
‘list’
:
-
if
n ==
0
:
-
return
res
-
else
:
-
if
len(res)<
2
:
-
res.append(
1
)
-
fibonacci(n-
1
, res)
-
else
:
-
last = res[-
1
]
-
second_last = res[-
2
]
-
res.append(last + second_last)
-
fibonacci(n-
1
, res)
-
return
res
-
print
(fibonacci(
8
))
-
print
(fibonacci.__annotations__)
:
Explanation:
Annotations for functions were used in the preceding code to generate the first list of n Fibonacci digits.
We also used the ‘__annotations__’ property to output the function annotation, and the result was a dictionary. 2. Typical Python Module Use:
To obtain the function annotations in Python, you can utilize the built-in pydoc package.
The pydoc.help() function creates a shell in which we may retrieve any file’s documentation, as well as the annotations for any function. 3. The Inspect Module in Action
Another one of Python’s built-in modules goes by the moniker “inspect.” Any file, module, class, or object can have its details displayed by this module.
There are a variety of data-gathering tools available. The getfullargspec method in the inspect module will be used to retrieve the function annotation details and any other relevant information.
information.
Example 3:
Final Product:
-
# Python program to print Fibonacci series using function annotations
-
import
inspect
-
def
fibonacci(n:
‘int’
, res:
‘list’
=[])->
‘list’
:
-
if
n ==
0
:
-
return
res
-
else
:
-
if
len(res)<
2
:
-
res.append(
1
)
-
fibonacci(n-
1
, res)
-
else
:
-
last = res[-
1
]
-
second_last = res[-
2
]
-
res.append(last + second_last)
-
fibonacci(n-
1
, res)
-
return
res
-
print
(fibonacci(
8
))
-
print
(inspect.getfullargspec(fibonacci))
Explanation:
By calling getfullargspec in the aforementioned code, we were able to obtain comprehensive information about the method, including the function’s annotation.