What does def main () -> None do?
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
.
Ianhi
Updated on March 24, 2020Comments
-
Ianhi over 4 years
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 todef main(argv=None)
?Beyond that, what is
->
in Python? I can't find it in this list of Python operators.