Document it or it doesn’t exist
Documentation is:
Comments made on the same line as code. Should be separated with 2 spaces.
Use sparingly. Explain why, not what.
Don’t use comments to mask other issues
Don’t use comments to mask other issues
Don’t state the obvious
Explain/justify unorthodoxy
Comment on attributes/variables meaning.
Although pep 224 was rejected, placing a string literal after an assignment is also popular. Using the attributes section on class or module docstring is probably the best approach, however.
class record(nt.void):
"""A data-type scalar that allows field
access as attribute lookup.
"""
# manually set name and module so that
# this class's type shows up as numpy.record
# when printed
__name__ = 'record'
__module__ = 'numpy'
Example from here , reformatted to fit on screen.
# GH#23758: We may still need to
# localize the result with tz
# GH#25546: Apply tz_parsed first (from arg),
# then tz (from caller)
# result will be naive but in UTC
result = (
result.tz_localize("UTC")
.tz_convert(tz_parsed)
)
Example from here, reformatted to fit on screen.
How can we make this comment better?
Delete it!
Docstrings follow functions, methods, modules, classes, etc. They explain the purpose and use of the code.
docstrings are accessed via __doc__
Ideal public (doesn’t start with _) docstrings:
There are several common flavors of docstrings
See the numpydoc site for more details on numpy flavored docstrings.
def recombobulate(bob, bits=None):
"""Create new Bob with bits put back.
Parameters
----------
bob : Bob
The Bob object which was discombobulated.
bits : Bits, Optional
The Bob bits which fell off.
Returns
-------
A new Bob object without missing bits.
Examples
--------
>>> new_bob = recombobulate(bob, bits)
"""
def factorial(n):
"""Return the factorial of n, an exact integer >= 0.
>>> factorial(30)
265252859812191058636308480000000
>>> [factorial(n) for n in range(6)]
[1, 1, 2, 6, 24, 120]
>>> factorial(-1)
Traceback (most recent call last):
...
ValueError: n must be >= 0
"""
This example comes from the doctest documentation
Directives
"""
>>> print(list(range(20))) # doctest: +NORMALIZE_WHITESPACE
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
"""
This example comes from the doctest documentation
Directives
This example comes from the doctest documentation
Example can (and should) be run to make sure it still works. Pytest does this:
What’s wrong here?
When to use a comment vs a docstring?
Your project’s elevator pitch / note in a bottle
Read more on the Diátaxis website.
Take the reader through a series of steps (learning-oriented).
Walk the reader through solving a real-world problem (goal-oriented).
Technical descriptions of the machinery and how to operate it (information-oriented).
Clarifies and illuminates a particular topic (understanding-oriented).
The most common libraries for building python docs:
The pages can be hosted on readthedocs, githubpages, netlify, or elsewhere.
With a partner, checkout two of these project’s readmes: