bgdev.docstring¶
Docstring template using Google convention.
- created
16/11/2018
- author
Benoit Gielly <benoit.gielly@gmail.com>
This module is just a template to show how to write docstrings properly using the Google’s syntax. It will show all the different use cases and keywords to use to get sphinx working nicely.
- You can find a link to the Google Docstring guide here:
http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html
Happy docstring-ing! :D
-
class
ExampleClass
(param1, param2, param3)[source]¶ 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’sArgs
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.-
attr1
¶ Description of attr1.
- Type
str
-
attr2
¶ Description of attr2.
- Type
int
, optional
-
__init__
(param1, param2, param3)[source]¶ Init method example docstring.
The __init__ should be documented here rather than the class docstring.
Note
Do not include the self parameter in the
Args
section.- Parameters
param1 (str) – Description of param1.
param2 (
int
) – Description of param2. Multiple lines are supported.param3 (list(str)) – Description of param3.
-
attr3
¶ Doc comment inline with attribute
-
attr4
¶ Doc comment before attribute, with type specified
- Type
list(str)
-
attr5
¶ Docstring after attribute, with type specified.
- Type
str
-
example_method
(param1, param2)[source]¶ Class methods are similar to regular functions.
See
example_function()
for a better description.Note
Do not include the self parameter in the
Args
section.
-
property
getter_only_property
¶ Properties should be documented in their getter method.
- Type
str
-
property
getter_setter_property
¶ Get a property.
If the setter method contains notable behavior, it should be mentioned here.
- Parameters
value (str) – Properties with both a getter and setter should only be documented in their getter method.
- Returns
The returned value/object.
- Return type
list
-
-
example_decorated_function
(arg)[source]¶ Test the docstring to ensure the @wraps decorator worked.
- Parameters
arg (str) – Description of arg1
- Returns
The value of arg1
- Return type
type
-
example_decorator
(func)[source]¶ Decorate a method.
Decorators need a special treatment to generate the documentation properly. You have to decorate the inner function with @wraps from functools or the docstring of the decorated function will be ignored.
-
example_function
(param1, param2=None, *args, **kwargs)[source]¶ Show 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.
- Parameters
param1 (int) – The first parameter.
param2 (str) – The second parameter. Defaults to None. Second line of description should be indented.
*args – Variable length argument list.
**kwargs – Arbitrary keyword arguments.
- Returns
True if successful, False otherwise.
The return type is optional and may be specified at the beginning of the
Returns
section followed by a colon.The
Returns
section may span multiple lines and paragraphs. Following lines should be indented to match the first line.The
Returns
section supports any reStructuredText formatting, including literal blocks:{ 'param1': param1, 'param2': param2 }
- Return type
bool
- Raises
AttributeError – The
Raises
section is a list of all exceptions that are relevant to the interface.ValueError – If param2 is equal to param1.
Example
Examples
should be written in doctest format, and should illustrate how to use the function.>>> print([i for i in range(4)]) [0, 1, 2, 3]
Examples
You can also use literal blocks:
print([i for i in range(4)]) >>> [0, 1, 2, 3]
Todo
The
Todo
section lists in an orange block every task that needs to be done.Make sure to use an * (asterisk) to display bullet points