Niladri Sekhar Basu Niladri Sekhar Basu - 1 month ago 7
Python Question

Use of `->` in Python?

Can anyone please tell me what is the difference between below two functions? They both look pretty similar except for



def foo1() -> None:


def foo2():

When I call them the act in similar manner. So why use
-> None
?. I know use of
of Python.


This is called type hinting, and is only used to help make it easier for the programmer to understand the return and input types of a program, as well as allow for external linting and static analysis programs to work on Python. Here is the official RFC explaining why it exists.

Type hints are not actually used by Python anyway (because Python is dynamically typed - the types are determined at runtime, not at compile time when the source is actually parsed) and can be safely omitted.

Functions conforming to you Version 2 are perfectly fine in every case.

Python 3 has inbuilt support for type hints with the typing module, while Python 2.7 requires the third-party mypy module.