5

Recently, I have come across this -> in Python 3 when studying function declarations. What does this do and mean? I have never seen such a declaration other than in a Javascript function declaration up until now.

def f(self, s: 'str') -> 'bool':
    pass
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Seungho Lee
  • 1,068
  • 4
  • 16
  • 42
  • 1
    Function `f` will return a boolean value. This enhances readability and makes it easy to debug. – taurus05 Feb 06 '19 at 05:59
  • Does this answer your question? [What does -> mean in Python function definitions?](https://stackoverflow.com/questions/14379753/what-does-mean-in-python-function-definitions) – Gino Mempin Oct 18 '22 at 09:20

2 Answers2

8

This is annotation for type of return value of function.

def sum() -> expression:

That is, the parameter list can now be followed by a literal -> and a Python expression. Like the annotations for parameters, this expression will be evaluated when the function definition is executed.

https://www.python.org/dev/peps/pep-3107/

Sach
  • 904
  • 8
  • 20
3

According to python docs related to typings.

This is the python typing feature, which let you specify the return type of functions in python

tbhaxor
  • 1,659
  • 2
  • 13
  • 43