diff -r ab94ed2a8012 Doc/tutorial/controlflow.rst --- a/Doc/tutorial/controlflow.rst Wed May 23 11:22:44 2012 +0200 +++ b/Doc/tutorial/controlflow.rst Wed May 23 14:09:51 2012 -0500 @@ -636,6 +636,39 @@ No, really, it doesn't do anything. +.. _tut-annotations: + +Function Annotations +-------------------- + +.. sectionauthor:: Zachary Ware +.. index:: + pair: function; annotations + single: -> (return annotation assignment) + +:ref:`Function annotations ` are completely optional, +arbitrary metadata information about user-defined functions. Python itself +currently does not use annotations for anything, so this section is just for +familiarity with the syntax. + +Annotations are stored in the :attr:`__annotations__` attribute of the function +as a dictionary and have no effect on any other part of the function. Parameter +annotations are defined by a colon after the parameter name, followed by an +expression evaluating to the value of the annotation. Return annotations are +defined by a literal ``->``, followed by an expression, between the parameter +list and the colon denoting the end of the :keyword:`def` statement. The +following example has a positional argument, a keyword argument, and the return +value annotated with nonsense:: + + >>> def f(ham: 42, eggs: int = 'spam') -> "Nothing to see here": + ... print("Annotations:", f.__annotations__) # print the function's own annotations + ... print("Arguments:", ham, eggs) + ... + >>> f('wonderful') + Annotations: {'eggs': , 'return': 'Nothing to see here', 'ham': 42} + Arguments: wonderful spam + + .. _tut-codingstyle: Intermezzo: Coding Style