Search code examples
pythonpython-3.xfunction-declaration

What does '->' mean in a function declaration in Python 3?


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

Solution

  • 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/