An Example to Write Google Style Docstrings¶
example_google
¶
Example Google style docstrings.
This module demonstrates documentation as specified by the Google Python
Style Guide_. Docstrings may extend over multiple lines. Sections are created
with a section header and a colon followed by a block of indented text.
Example
Examples can be given using either the Example or Examples
sections. Sections support any reStructuredText formatting, including
literal blocks::
$ python example_google.py
Section breaks are created by resuming unindented text. Section breaks are also implicitly created anytime a new section starts.
| ATTRIBUTE | DESCRIPTION |
|---|---|
module_level_variable1 |
Module level variables may be documented in
either the Either form is acceptable, but the two should not be mixed. Choose one convention to document module level variables and be consistent with it.
TYPE:
|
Todo
- For module TODOs
- You have to also use
sphinx.ext.todoextension
.. _Google Python Style Guide: http://google.github.io/styleguide/pyguide.html
Attributes¶
module_level_variable2
module-attribute
¶
module_level_variable2 = 98765
int: Module level variable documented inline.
The docstring may span multiple lines. The type may optionally be specified on the first line, separated by a colon.
Classes¶
ExampleClass
¶
ExampleClass(param1, param2, param3)
Bases: object
The summary line for a class docstring should fit on one line.
If the class has public attributes, they may be documented here
in an Attributes section and follow the same formatting as a
function's Args section. Alternatively, attributes may be documented
inline with the attribute's declaration (see init method below).
Properties created with the @property decorator should be documented
in the property's getter method.
| ATTRIBUTE | DESCRIPTION |
|---|---|
attr1 |
Description of
TYPE:
|
attr2 |
obj:
|
Example of docstring on the init method.
The init method may be documented in either the class level docstring, or as a docstring on the init method itself.
Either form is acceptable, but the two should not be mixed. Choose one convention to document the init method and be consistent with it.
Note
Do not include the self parameter in the Args section.
| PARAMETER | DESCRIPTION |
|---|---|
param1 |
Description of
TYPE:
|
param2 |
obj:
|
param3 |
obj:
|
Source code in Tairitsu/example_google.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
Attributes¶
readonly_property
property
¶
readonly_property
str: Properties should be documented in their getter method.
readwrite_property
property
writable
¶
readwrite_property
:obj:list of :obj:str: Properties with both a getter and setter
should only be documented in their getter method.
If the setter method contains notable behavior, it should be mentioned here.
Functions¶
example_method
¶
example_method(param1, param2)
Class methods are similar to regular functions.
Note
Do not include the self parameter in the Args section.
| PARAMETER | DESCRIPTION |
|---|---|
param1 |
The first parameter.
|
param2 |
The second parameter.
|
| RETURNS | DESCRIPTION |
|---|---|
|
True if successful, False otherwise. |
Source code in Tairitsu/example_google.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | |
ExampleError
¶
ExampleError(msg, code)
Bases: Exception
Exceptions are documented in the same way as classes.
The init method may be documented in either the class level docstring, or as a docstring on the init method itself.
Either form is acceptable, but the two should not be mixed. Choose one convention to document the init method and be consistent with it.
Note
Do not include the self parameter in the Args section.
| PARAMETER | DESCRIPTION |
|---|---|
msg |
Human readable string describing the exception.
TYPE:
|
code |
obj:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
msg |
Human readable string describing the exception.
TYPE:
|
code |
Exception error code.
TYPE:
|
Source code in Tairitsu/example_google.py
176 177 178 | |
Functions¶
example_generator
¶
example_generator(n)
Generators have a Yields section instead of a Returns section.
| PARAMETER | DESCRIPTION |
|---|---|
n |
The upper limit of the range to generate, from 0 to
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
int
|
The next number in the range of 0 to |
Examples:
Examples should be written in doctest format, and should illustrate how to use the function.
>>> print([i for i in example_generator(4)])
[0, 1, 2, 3]
Source code in Tairitsu/example_google.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
function_with_pep484_type_annotations
¶
function_with_pep484_type_annotations(param1: int, param2: str) -> bool
Example function with PEP 484 type annotations.
| PARAMETER | DESCRIPTION |
|---|---|
param1 |
The first parameter.
TYPE:
|
param2 |
The second parameter.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
The return value. True for success, False otherwise. |
Source code in Tairitsu/example_google.py
66 67 68 69 70 71 72 73 74 75 76 | |
function_with_types_in_docstring
¶
function_with_types_in_docstring(param1, param2)
Example function with types documented in the docstring.
PEP 484 type annotations are supported. If attribute, parameter, and
return types are annotated according to PEP 484, they do not need to be
included in the docstring:
| PARAMETER | DESCRIPTION |
|---|---|
param1 |
The first parameter.
TYPE:
|
param2 |
The second parameter.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
The return value. True for success, False otherwise. |
.. _PEP 484: https://www.python.org/dev/peps/pep-0484/
Source code in Tairitsu/example_google.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
module_level_function
¶
module_level_function(param1, param2=None, *args, **kwargs)
This is an example of a module level function.
Function parameters should be documented in the Args section. The name
of each parameter is required. The type and description of each parameter
is optional, but should be included if not obvious.
If *args or **kwargs are accepted,
they should be listed as *args and **kwargs.
The format for a parameter is::
name (type): description
The description may span multiple lines. Following
lines should be indented. The "(type)" is optional.
Multiple paragraphs are supported in parameter
descriptions.
| PARAMETER | DESCRIPTION |
|---|---|
param1 |
The first parameter.
TYPE:
|
param2 |
obj:
DEFAULT:
|
*args |
Variable length argument list.
DEFAULT:
|
**kwargs |
Arbitrary keyword arguments.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if successful, False otherwise. |
|
The return type is optional and may be specified at the beginning of |
|
|
the |
|
|
The |
|
|
Following lines should be indented to match the first line. |
|
|
The |
|
|
including literal blocks:: { 'param1': param1, 'param2': param2 } |
| RAISES | DESCRIPTION |
|---|---|
AttributeError
|
The |
ValueError
|
If |
Source code in Tairitsu/example_google.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |