OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

What does -> mean in Python function definitions?

  • Thread starter Thread starter Krotton
  • Start date Start date
K

Krotton

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

Code:
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:

Code:
def f(x) -> 123:
    return x

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

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

Could anyone familiar with this syntax style explain it?
<p>I've recently noticed something interesting when looking at <a href="http://docs.python.org/3.3/reference/grammar.html" rel="noreferrer">Python 3.3 grammar specification</a>:</p>
<pre><code>funcdef: 'def' NAME parameters ['->' test] ':' suite
</code></pre>
<p>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:</p>
<pre><code>def f(x) -> 123:
return x
</code></pre>
<p>I thought that this might be some kind of a precondition syntax, but:</p>
<ul>
<li>I cannot test <code>x</code> here, as it is still undefined,</li>
<li>No matter what I put after the arrow (e.g. <code>2 < 1</code>), it doesn't affect the function behavior.</li>
</ul>
<p>Could anyone familiar with this syntax style explain it?</p>
 

Latest posts

Top