Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
1491cbdf
Commit
1491cbdf
authored
Nov 01, 2012
by
Andrew Svetlov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #14893: Add function annotation example to function tutorial.
Patch by Zachary Ware.
parent
e4ad37e5
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
0 deletions
+34
-0
Doc/tutorial/controlflow.rst
Doc/tutorial/controlflow.rst
+34
-0
No files found.
Doc/tutorial/controlflow.rst
View file @
1491cbdf
...
...
@@ -656,6 +656,40 @@ Here is an example of a multi-line docstring::
No, really, it doesn't do anything.
.. _tut-annotations:
Function Annotations
--------------------
.. sectionauthor:: Zachary Ware <zachary.ware@gmail.com>
.. index::
pair: function; annotations
single: -> (return annotation assignment)
:ref:`Function annotations <function>` are completely optional,
arbitrary metadata information about user-defined functions. Neither Python
itself nor the standard library use function annotations in any way; this
section just shows the syntax. Third-party projects are free to use function
annotations for documentation, type checking, and other uses.
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("Arguments:", ham, eggs)
...
>>> f('wonderful')
Annotations: {'eggs': <class 'int'>, 'return': 'Nothing to see here', 'ham': 42}
Arguments: wonderful spam
.. _tut-codingstyle:
Intermezzo: Coding Style
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment