What does ' -> ' mean in Python function definitions?

0 votes

've recently noticed something interesting when looking at Python 3.3 grammar specification:

funcdef: 'def' NAME parameters ['->' test] ':' suite

The optional 'arrow' block was absent in Python 2 and I couldn't find any information regarding its meaning in Python 3. It turns out this is correct Python and it's accepted by the interpreter:

def f(x) -> 123:
    return x

I thought that this might be some kind of a precondition syntax, but:

  • I cannot test x here, at it is still undefined,
  • No matter what I put after the arrow (e.g. 2 < 1), it doesn't affect the function behaviour.

Could anyone accustomed with this syntax explain it?

May 22, 2018 in Python by ariaholic
• 7,340 points
64 views

1 answer to this question.

0 votes

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.

answered May 22, 2018 by charlie_brown
• 7,720 points

Related Questions In Python

0 votes
1 answer

What does calling a function means in Python?

Calling a function means that you are ...READ MORE

answered Dec 17, 2018 in Python by Shuvodip
50 views
0 votes
1 answer

What does the random.triangular(low, high, mode) function do in python?

It returns a random floating point number ...READ MORE

answered May 27 in Python by Vinod
93 views
0 votes
1 answer

What does numpy.reshape() function do in python?

numpy.reshape() gives a new shape to an ...READ MORE

answered May 29 in Python by Kyraa
105 views
0 votes
1 answer

What does super() function do in python?

Super function calls the parent class method ...READ MORE

answered Aug 2 in Python by Mohammad
• 3,060 points
39 views
0 votes
1 answer

What does the dir() function do in python?

The dir() function returns all properties and methods of ...READ MORE

answered Aug 6 in Python by Mohammad
• 3,060 points
28 views
+1 vote
3 answers
0 votes
1 answer

What is the use of raw_input function in Python?

raw_input fuction is no longer available in ...READ MORE

answered May 2, 2018 in Python by aayushi
• 750 points
121 views
+1 vote
1 answer

What is the function for Factorial in Python

Easiest way: math.factorial(x) (available in 2.6 and ...READ MORE

answered Aug 21, 2018 in Python by Priyaj
• 56,940 points

edited Aug 21, 2018 by Omkar 160 views
0 votes
1 answer

What does hash do in python?

A hash is an fixed sized integer ...READ MORE

answered Nov 14, 2018 in Python by charlie_brown
• 7,720 points
65 views
0 votes
1 answer

Is there a foreach function in python and is there a way to implement it if there isnt any

Every occurence of "foreach" I've seen (PHP, ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,720 points
88 views