Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(2271)

Side by Side Diff: Doc/tutorial/controlflow.rst

Issue 14893: Tutorial: Add function annotation example to function tutorial
Patch Set: Created 12 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 .. _tut-morecontrol: 1 .. _tut-morecontrol:
2 2
3 *********************** 3 ***********************
4 More Control Flow Tools 4 More Control Flow Tools
5 *********************** 5 ***********************
6 6
7 Besides the :keyword:`while` statement just introduced, Python knows the usual 7 Besides the :keyword:`while` statement just introduced, Python knows the usual
8 control flow statements known from other languages, with some twists. 8 control flow statements known from other languages, with some twists.
9 9
10 10
(...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after
627 ... """Do nothing, but document it. 627 ... """Do nothing, but document it.
628 ... 628 ...
629 ... No, really, it doesn't do anything. 629 ... No, really, it doesn't do anything.
630 ... """ 630 ... """
631 ... pass 631 ... pass
632 ... 632 ...
633 >>> print(my_function.__doc__) 633 >>> print(my_function.__doc__)
634 Do nothing, but document it. 634 Do nothing, but document it.
635 635
636 No, really, it doesn't do anything. 636 No, really, it doesn't do anything.
637
638
639 .. _tut-annotations:
640
641 Function Annotations
642 --------------------
643
644 .. sectionauthor:: Zachary Ware <zachary.ware@gmail.com>
645 .. index::
646 pair: function; annotations
647 single: -> (return annotation assignment)
648
649 :ref:`Function annotations <function>` are completely optional,
eric.araujo 2012/05/25 07:39:39 The ref target looks wrong. Maybe you want a link
zach.ware 2012/05/25 19:23:23 I had the same thought initially, but then I figur
650 arbitrary metadata information about user-defined functions. Python itself
651 currently does not use annotations for anything, so this section is just for
652 familiarity with the syntax.
eric.araujo 2012/05/25 07:39:39 Wording improvement suggestion: “Python itself or
zach.ware 2012/05/25 19:23:23 Done, though I moved the negative and added a comm
653
654 Annotations are stored in the :attr:`__annotations__` attribute of the function
655 as a dictionary and have no effect on any other part of the function. Parameter
656 annotations are defined by a colon after the parameter name, followed by an
657 expression evaluating to the value of the annotation. Return annotations are
658 defined by a literal ``->``, followed by an expression, between the parameter
659 list and the colon denoting the end of the :keyword:`def` statement. The
660 following example has a positional argument, a keyword argument, and the return
661 value annotated with nonsense::
662
663 >>> def f(ham: 42, eggs: int = 'spam') -> "Nothing to see here":
664 ... print("Annotations:", f.__annotations__) # print the function's own a nnotations
eric.araujo 2012/05/25 07:39:39 The comment seems to merely duplicate the code, wh
zach.ware 2012/05/25 19:23:23 Good point. I think this was a holdover from a pr
665 ... print("Arguments:", ham, eggs)
666 ...
667 >>> f('wonderful')
668 Annotations: {'eggs': <class 'int'>, 'return': 'Nothing to see here', 'ham': 42}
669 Arguments: wonderful spam
637 670
638 671
639 .. _tut-codingstyle: 672 .. _tut-codingstyle:
640 673
641 Intermezzo: Coding Style 674 Intermezzo: Coding Style
642 ======================== 675 ========================
643 676
644 .. sectionauthor:: Georg Brandl <georg@python.org> 677 .. sectionauthor:: Georg Brandl <georg@python.org>
645 .. index:: pair: coding; style 678 .. index:: pair: coding; style
646 679
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 slightest chance people speaking a different language will read or maintain 722 slightest chance people speaking a different language will read or maintain
690 the code. 723 the code.
691 724
692 725
693 .. rubric:: Footnotes 726 .. rubric:: Footnotes
694 727
695 .. [#] Actually, *call by object reference* would be a better description, 728 .. [#] Actually, *call by object reference* would be a better description,
696 since if a mutable object is passed, the caller will see any changes the 729 since if a mutable object is passed, the caller will see any changes the
697 callee makes to it (items inserted into a list). 730 callee makes to it (items inserted into a list).
698 731
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7