Function annotations() in Python
What is Function Annotations?
Function annotations provide a way related to different parts of a function at compile time with arbitrary Python expressions. It doesn’t have life in Python runtime execution. These annotations do not have any meaning in Python. Function annotations are completely choice for return value and parameters.
Function annotations are added to Python as PEP-3107. PEP stands for Python Enhancement Proposal. The main reason for having annotations in Python is for linking metadata to function that returns value and parameters.
Syntax:
As we mention above, parameters are optional for parameter name in Python.
So, we are using word ‘expression’ is a type of parameter that can be use by external libraries in arbitrary string.
Annotations of simple parameters:
We use argument name as ‘:’ which is given by the expression. Syntax for annotation can be expressed as:
def foo (a: statement, b: statement = 8):
Annotations for excess parameters:
Excess parameter will perform arbitrary number of arguments to be sent in a function call. Examples for excess parameter are args and kwargs. Excess parameter syntax has shown below:
def foo(*args: statement, *kwargs: statement):
Annotations for nested parameters:
Nested parameters are used when a tuple is called in a function call then the feature of Python 2x will automatically open. Where as in Python 3x, it is removed and when it requires, we can open it manually.
Syntax for nested parameters before tuple as shown below:
def foo ((a: expression, b: expression), (c: expression, d: expression)):
Annotations for return type:
In return type annotations, ‘->’ sign is used which is further passed by ‘:’. Annotation return type is different from function arguments. Return type annotation syntax has shown below:
def foobar (a: statement)->statement:
Example program for function annotations:
// Python program to demonstrate Fibonacci series
deffib(j:'int', output: ‘list'= [])-> 'list':
ifj ==0:
returnoutput
else:
iflen(output)< 2:
output. append (1)
fib (j-1, output)
else:
last =output [-1]
second last =output [-2]
output. append (last +second_last)
fib (j-1, output)
returnoutput
print (fib (8))
Output:
[1, 1, 2, 3, 5, 8]
Default Arguments:
//Default arguments program has shown below
def foobar(j: 'an argument that defaults to 5' = 5):
print(j)
foobar(7)
7
foobar()
5
Accessing Function Annotations
- Using ‘_annotations_’:
The function annotations from the above codes can be accessed to allocate ‘_annotations_’. We use a special keyword ‘return’ for display an output. The following code will print the annotations.
//Python program to demonstrate function annotations:
deffib(j:'int', output:'list'=[])-> 'list':
ifj ==0:
returnoutput
else:
iflen (output)< 2:
output. append (1)
fib (j-1, output)
else:
last =output [-1]
second_last =output [-2]
output. Append (last +second_last)
fib (j-1, output)
returnoutput
print(fib. __annotations__)
Output:
{‘return’: ‘list’, ‘j’: ‘int’, ‘output’: ‘list’}
- Using standard module ‘pydoc’:
The ‘pydoc’ is a primary Python module that gives documentation inside a Python module. It has a special ‘help ()’ method that gives help to the methods or keywords. The ‘help ()’ method is used in function annotation.
- Using standard module ‘inspect’:
The ‘inspect’ module gives useful functions to help the information about live objects. Examples for standard modules are classes, functions, modules, methods, code objects etc. The ‘getfullargspec’ method is used to get complete information about the function which has function annotations.
//Python program to demonstrate function annotations
importinspect
deffib(j:'int', output: ‘list'=[])-> 'list':
ifj ==0:
returnoutput
else:
iflen(output)< 2:
output. append (1)
fib(j-1, output)
else:
last =output[-1]
second_last =output[-2]
output. append (last +second_last)
fib(j-1, output)
returnoutput
print (inspect. getfullargspec(fib))
Output:
FullArgSpec(args=['n', 'output'], varargs=None,
varkw=None, defaults=([], ), kwonlyargs=[],
kwonlydefaults=None, annotations=
{'output': 'list', 'return': 'list', 'n': 'int'})