![]() |
VOOZH | about |
Basic Terminology
PEP: PEP stands for Python Enhancement Proposal. It is a design document that describes new features for Python or its processes or environment. It also provides information to the python community. PEP is a primary mechanism for proposing major new features, for example - Python Web Server Gateway Interface, collecting the inputs of the community on the issues and documenting design decisions that have been implemented in Python. Function Annotations β PEP 3107 : PEP-3107 introduced the concept and syntax for adding arbitrary metadata annotations to Python. It was introduced in Python3 which was previously done using external libraries in python 2.xWhat are Function annotations?
Function annotations are arbitrary python expressions that are associated with various part of functions. These expressions are evaluated at compile time and have no life in pythonβs runtime environment. Python does not attach any meaning to these annotations. They take life when interpreted by third party libraries, for example, mypy. Purpose of function annotations: The benefits from function annotations can only be reaped via third party libraries. The type of benefits depends upon the type of the library, for example[def foo(a:βintβ, b:βfloatβ=5.0) -> βintβ](syntax described in detail in the next section) can be used to collect information about the type of the parameters and the return type of the function to keep track of the type change occurring in the function. βmypyβ is one such library.
Syntax of function annotations
They are like the optional parameters that follow the parameter name.def foobar(a: expression, b: expression = 5):
def foobar(*args: expression, *kwargs: expression):
def foobar((a: expression, b: expression), (c: expression, d: expression)):
def foobar(a: expression)->expression:
decorator : β@β name_ [β(β [arglist] β)β] NEWLINE decorators : decorator+ funcdef : [decorators] βdefβ NAME parameters [β->β] β:β suite parameters : β(β [typedarglist] β)β typedarglist : (( tfpdef [β=β test] β, β)* (β*β [tname] (β, β tname [β=β test])* [β, β β **β tname] | β**β tname) | tfpdef [β=β test (β, β tfpdef [β=β test])* [β, β]]) tname : NAME [β:β test] tfpdef : tname | β(β tfplist β)β tfplist : tfpdef (β, β tfpdef)* [β, β]Visualizing Grammar : The parse tree is formed from the above grammar to give better visualization of the syntax of the python's function and function annotations. π Image
Output: [1, 1, 2, 3, 5]Note: Function annotations are only supported in python 3x.
Accessing Function Annotations
1. Using '__annotations__' : The function annotations in the above code can be accessed by a special attribute '__annotations__'. It outputs the dictionary having a special key 'return' and other keys having name of the annotated arguments. The following code will print the annotations.Output: {'return': 'list', 'n': 'int', 'output': 'list'}
2. Using standard module 'pydoc' : The 'pydoc' is a standard python module that returns the documentation inside a python module(if any). It has a special 'help()' method that provides an interactive shell to get help on any keyword, method, class or module. 'help()' can be used to access the function annotations. The image below shows the function annotations in the above Fibonacci series code. The module name is 'fib.py'.
π ImageOutput: FullArgSpec(args=['n', 'output'], varargs=None,
varkw=None, defaults=([], ), kwonlyargs=[],
kwonlydefaults=None, annotations=
{'output': 'list', 'return': 'list', 'n': 'int'})
Application of Function Annotations
pip install mypypython 3x
pip install git+git://github.com/JukkaL/mypy.gitExample 1: Save the above code as example.py and run the following command after installation of mypy. Make sure you are in the directory where you have saved the file.
mypy example.pyYou will get the following result. π Image