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
2
  • 2
    Function f will return a boolean value. This enhances readability and makes it easy to debug. Commented Feb 6, 2019 at 5:59
  • Does this answer your question? What does -> mean in Python function definitions? Commented Oct 18, 2022 at 9:20

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.