What the "->" sign means for Python functions?

ElkelK report abuse

I have noticed that sometimes in Python functions after the closing brackets with parameters the "->" sign is written. After this sign some value is specified, and only after this value there is a colon and the body of the function begins. So, for example, I speak about this:

def my_function(parameter1, parameter2) -> 'some string value': pass

What is this syntax for?


Zuska report abuse

This is a type of documenting Python programs. You may know that there is a notion of the docstring, which, if put simply, is a description of the function. You can also comment on input parameters to the function and the output value. The sign '->' is used to comment on the object the function returns. For example:

def aggreg(month1, month2) -> 'The return value is the total amount of items sold, measured in kg': return month1['soldamount'] + month2['soldamount']

This function returns the total amount of items sold for 2 months. We noted this information after the '->' sign.

ElkelK report abuse

Interesting. You have said that there is a way to comment on input parameters also. How we can do this?

Zuska report abuse __ edited

You can read this and a lot more information about the topic here - https://www.python.org/dev/peps/pep-3107/ . In short, you can use a colon, like in the following function: def aggreg(month1:'description of the first parameter', month2:'description of the second parameter') -> 'description of the return val': return month1['soldamount'] + month2['soldamount']

ElkelK report abuse __ edited


Add Answer

Need support?

Just drop us an email to ... Show more