What does -> mean in Python function definitions?
In Python, the "->" symbol is used to indicate the return type of a function. It is part of the function definition in a Python 3.5 or later. For example, the following code defines a function add that takes in two arguments, a and b, and returns the sum of the two arguments.
def add(a: int, b: int) -> int:
return a + b
The ->
int at the end of the function definition indicates that the function returns an integer. This is known as type hinting and is used to help with code readability and debugging.
Please note that this feature is not enforced in Python and it's just used as a hint for developer and IDEs.
It's a function annotation.
In more detail, Python 2.x has docstrings, which allow you to attach a metadata string to various types of object. This is amazingly handy, so Python 3 extends the feature by allowing you to attach metadata to functions describing their parameters and return values.
There's no preconceived use case, but the PEP suggests several. One very handy one is to allow you to annotate parameters with their expected types; it would then be easy to write a decorator that verifies the annotations or coerces the arguments to the right type. Another is to allow parameter-specific documentation instead of encoding it into the docstring.