What does def main () -> None do?

Question:

I am working through Mastering Matplotlib and in chapter two they introduce the following code snippet:

#! /usr/bin/env python3.4
import matplotlib.pyplot as plt

def main () -> None:
    plt.plot([1,2,3,4])
    plt.ylabel('some numbers')
    plt.savefig('simple-line.png')

if __name__ == '__main__':
    main()

This can be seen in this notebook, cell 10. I have never seen a main method defined this way, what is the function of -> None?
My only thought so far is that this may be similar to def main(argv=None)?

Beyond that, what is -> in Python? I can’t find it in this list of Python operators.

Asked By: Ianhi

||

Answers:

As is, it does absolutely nothing. It is a type annotation for the main function that simply states that this function returns None. Type annotations were introduced in Python 3.5 and are specified in PEP 484.

Annotations for the return value of a function use the symbol -> followed by a type. It is completely optional and if you removed it, nothing would change.

This will have absolutely no effect on execution, it is only taken under consideration if you use it with a type checking tool like mypy.

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.