| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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 628 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 639 .. _tut-annotations: | 639 .. _tut-annotations: |
| 640 | 640 |
| 641 Function Annotations | 641 Function Annotations |
| 642 -------------------- | 642 -------------------- |
| 643 | 643 |
| 644 .. sectionauthor:: Zachary Ware <zachary.ware@gmail.com> | 644 .. sectionauthor:: Zachary Ware <zachary.ware@gmail.com> |
| 645 .. index:: | 645 .. index:: |
| 646 pair: function; annotations | 646 pair: function; annotations |
| 647 single: -> (return annotation assignment) | 647 single: -> (return annotation assignment) |
| 648 | 648 |
| 649 :ref:`Function annotations <function>` are completely optional, | 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 | 650 arbitrary metadata information about user-defined functions. Neither Python |
| 651 currently does not use annotations for anything, so this section is just for | 651 itself nor the standard library use function annotations in any way; this |
| 652 familiarity with the syntax. | 652 section just shows the syntax. Third-party projects are free to use function |
|
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 annotations for documentation, type checking, and other uses. | |
| 653 | 654 |
| 654 Annotations are stored in the :attr:`__annotations__` attribute of the function | 655 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 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 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 expression evaluating to the value of the annotation. Return annotations are |
| 658 defined by a literal ``->``, followed by an expression, between the parameter | 659 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 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 following example has a positional argument, a keyword argument, and the return |
| 661 value annotated with nonsense:: | 662 value annotated with nonsense:: |
| 662 | 663 |
| 663 >>> def f(ham: 42, eggs: int = 'spam') -> "Nothing to see here": | 664 >>> def f(ham: 42, eggs: int = 'spam') -> "Nothing to see here": |
| 664 ... print("Annotations:", f.__annotations__) # print the function's own a nnotations | 665 ... print("Annotations:", f.__annotations__) |
|
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 ... print("Arguments:", ham, eggs) |
| 666 ... | 667 ... |
| 667 >>> f('wonderful') | 668 >>> f('wonderful') |
| 668 Annotations: {'eggs': <class 'int'>, 'return': 'Nothing to see here', 'ham': 42} | 669 Annotations: {'eggs': <class 'int'>, 'return': 'Nothing to see here', 'ham': 42} |
| 669 Arguments: wonderful spam | 670 Arguments: wonderful spam |
| 670 | 671 |
| 671 | 672 |
| 672 .. _tut-codingstyle: | 673 .. _tut-codingstyle: |
| 673 | 674 |
| 674 Intermezzo: Coding Style | 675 Intermezzo: Coding Style |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 722 slightest chance people speaking a different language will read or maintain | 723 slightest chance people speaking a different language will read or maintain |
| 723 the code. | 724 the code. |
| 724 | 725 |
| 725 | 726 |
| 726 .. rubric:: Footnotes | 727 .. rubric:: Footnotes |
| 727 | 728 |
| 728 .. [#] Actually, *call by object reference* would be a better description, | 729 .. [#] Actually, *call by object reference* would be a better description, |
| 729 since if a mutable object is passed, the caller will see any changes the | 730 since if a mutable object is passed, the caller will see any changes the |
| 730 callee makes to it (items inserted into a list). | 731 callee makes to it (items inserted into a list). |
| 731 | 732 |
| LEFT | RIGHT |