Make delicious recipes!

Function annotations

Function annotations are not used by the python compiler but can be used by other programs to extract some meaningful information.
Syntax for specifying an annotation is:
  1. For an argument, it is : <annotation>
  2. For a return argument, it is -> <annotation> :
Note that the position of ':' is different in both the cases.

__annotations__ can be used to print the annotations of a function.


# below function has 3 annotations: 2 for each argument, and one for the return argument.
>>> def func (arg1 : 'some-annotation', arg2 : 50 = 'hiii') -> ('tple', 'annotation'):
...     print (func.__annotations__)

# call the function to print all the annotations
>>> func ('okkk')
{'return': ('tple', 'annotation'), 'arg2': 50, 'arg1': 'some-annotation'}

Like us on Facebook to remain in touch
with the latest in technology and tutorials!

Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Email: (Your email is not shared with anybody)

Facebook comments:

Site Owner: Sachin Goyal