[issue14893] Tutorial: Add function annotation example to function tutorial

New submission from Zachary Ware <zachary.ware@gmail.com>: A couple months ago, I had never before heard of function annotations and came across a function that had them (I don't remember where or what it was). I spent a fair bit of time searching fruitlessly to figure out what the heck that "->" in the function definition meant. Then finally, a week or two ago, I came across a mention of pep 3107 and function annotations, and figured out what it was that confused me so thoroughly. In an effort to save others from the same confusion, I've put together a small subsection to add to the tutorial about function definition. The text I'm proposing to add is as follows: """ :ref:`Function annotations <function>` 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': <class 'int'>, 'return': 'Nothing to see here', 'ham': 42} Arguments: wonderful spam """
I'd also like to see a link for "->" in the index, either to this note or to the relevant paragraph of compound_stmts.rst or both. The attached patch attempts to add such a link to this section, but I'm not certain that it's done properly. Thanks :) ---------- assignee: docs@python components: Documentation files: annotations_tutorial.patch keywords: patch messages: 161451 nosy: docs@python, zach.ware priority: normal severity: normal status: open title: Tutorial: Add function annotation example to function tutorial type: enhancement versions: Python 3.2, Python 3.3 Added file: http://bugs.python.org/file25684/annotations_tutorial.patch _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14893> _______________________________________

Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: This looks like a reasonable addition to the tutorial :-) ---------- nosy: +rhettinger _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14893> _______________________________________

Changes by Ezio Melotti <ezio.melotti@gmail.com>: ---------- nosy: +eric.araujo, ezio.melotti stage: -> patch review _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14893> _______________________________________

Éric Araujo <merwok@netwok.org> added the comment: Thanks for the patch. I made some comments on the code review tool, which should have sent you a mail. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14893> _______________________________________

Zachary Ware <zachary.ware@gmail.com> added the comment: Thanks for the review :). Replied and here's the updated patch. ---------- Added file: http://bugs.python.org/file25708/annotations_tutorial.v2.patch _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14893> _______________________________________

Éric Araujo <merwok@netwok.org> added the comment: LGTM, will commit. ---------- assignee: docs@python -> eric.araujo _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14893> _______________________________________

Changes by Zachary Ware <zachary.ware@gmail.com>: ---------- versions: +Python 3.4 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14893> _______________________________________

Roundup Robot added the comment: New changeset 2bf99322218f by Andrew Svetlov in branch '3.2': Issue #14893: Add function annotation example to function tutorial. http://hg.python.org/cpython/rev/2bf99322218f New changeset 45167091b5f9 by Andrew Svetlov in branch '3.3': Merge issue #14893: Add function annotation example to function tutorial. http://hg.python.org/cpython/rev/45167091b5f9 New changeset 63b495ff366b by Andrew Svetlov in branch 'default': Merge issue #14893: Add function annotation example to function tutorial. http://hg.python.org/cpython/rev/63b495ff366b ---------- nosy: +python-dev _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14893> _______________________________________

Andrew Svetlov added the comment: Fixed. Thanks, Zachary. ---------- nosy: +asvetlov resolution: -> fixed stage: patch review -> committed/rejected status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14893> _______________________________________

Zachary Ware added the comment: Thank you Éric for the approval, and Andrew for the commit! ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14893> _______________________________________
participants (6)
-
Andrew Svetlov
-
Ezio Melotti
-
Raymond Hettinger
-
Roundup Robot
-
Zachary Ware
-
Éric Araujo