38

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.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
Ianhi
  • 2,864
  • 1
  • 25
  • 24
  • 3
    https://docs.python.org/3/library/typing.html It's a type hint for the return type of the function. – pvg Jul 09 '16 at 22:01
  • It is similar to creating a `void` function in Java. It is python's way of starting to incorporate types in to make the language more robust. – timotaoh Mar 07 '22 at 00:05

1 Answers1

79

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.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
  • so it's possible to turn python scripts into partially, statically typed programs which will be faster than the regular, dynamically typed python? if yes, is it a big difference? should I familiarise myself with this if I run data analysis on large data sets? – J.Doe Dec 29 '22 at 01:17