Skip to content

Commit 4fb3bf7

Browse files
Wrote a bit about docstrings
1 parent 91e2085 commit 4fb3bf7

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

docs/writing/documentation.rst

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ There is also **great**, **free** hosting for your Sphinx_ docs:
6565
your source repository so that rebuilding your documentation will
6666
happen automatically.
6767

68+
When run, Sphinx_ will import your code and using Python's introspection
69+
features it will extract all function, method and class signatures. It will
70+
also extract the accompanying docstrings, and compile it all into well
71+
structured and easily readable documentation for your project.
72+
6873
.. note::
6974

7075
Sphinx is famous for its API generation, but it also works well
@@ -127,6 +132,30 @@ Some tools use docstrings to embed more-than-documentation behavior,
127132
such as unit test logic. Those can be nice, but you won't ever go
128133
wrong with vanilla "here's what this does."
129134

135+
Tools like Sphinx_ will parse your docstrings as reStructuredText and render it
136+
correctly as HTML. This makes it very easy to embed snippets of example code in
137+
a project's documentation.
138+
139+
Additionally, Doctest_ will read all embedded docstrings that look like input
140+
from the Python commandline (prefixed with ">>>") and run them, checking to see
141+
if the output of the command matches the text on the following line. This
142+
allows developers to embed real examples and usage of functions alongside
143+
their source code, and as a side effect, it also ensures that their code is
144+
tested and works.
145+
146+
::
147+
148+
def my_function(a, b):
149+
"""
150+
>>> my_function(2, 3)
151+
6
152+
>>> my_function('a', 3)
153+
'aaa'
154+
"""
155+
return a * b
156+
157+
.. _Doctest: https://docs.python.org/3/library/doctest.html
158+
130159
Docstrings versus Block comments
131160
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
132161

@@ -141,8 +170,99 @@ comment block is a programmer's note. The docstring describes the
141170
"""Returns the square root of self times self."""
142171
...
143172
173+
Unlike block comments, docstrings are built into the Python language itself.
174+
This means you can use all of Python's powerful introspection capabilities to
175+
access docstrings at runtime, compared with comments which are optimised out.
176+
Docstrings are accessible from both the `__doc__` dunder attribute for almost
177+
every Python object, as well as with the built in `help()` function.
178+
179+
While block comments are usually used to explain *what* a section of code is
180+
doing, or the specifics of an algorithm, docstrings are more intended for
181+
explaining to other users of your code (or you in 6 months time) *how* a
182+
particular function can be used and the general purpose of a function, class,
183+
or module.
184+
185+
Writing Docstrings
186+
~~~~~~~~~~~~~~~~~~
187+
188+
Depending on the complexity of the function, method, or class being written, a
189+
one-line docstring may be perfectly appropriate. These are generally used for
190+
really obvious cases, such as::
191+
192+
def add(a, b):
193+
"""Add two numbers and return the result."""
194+
return a + b
195+
196+
The docstring should describe the function in a way that is easy to understand.
197+
Embedding the function's signature in the docstring is unnecessary because it
198+
can easily be obtained using the `inspect` module, and doesn't provide much
199+
additional information.
200+
201+
For more complex cases, there are a couple generally accepted styles used
202+
when writing documentation. The first of these uses reStructuredText syntax
203+
to format arguments and other elements of the docstring appropriately::
204+
205+
def function1(self, arg1, arg2, arg3):
206+
"""A short, one line summary of the function.
207+
208+
This is a longer explanation, which may include math with
209+
latex syntax :math:`\\alpha`.
210+
211+
:param arg1: the first value
212+
:param arg2: the first value
213+
:param arg3: the first value
214+
:type arg1: int, float,...
215+
:type arg2: int, float,...
216+
:type arg3: int, float,...
217+
:returns: arg1/arg2 +arg3
218+
:rtype: int, float
219+
"""
220+
return arg1/arg2 + arg3
221+
222+
`thomas-cokelaer.info`_ has a fairly complete article showing more examples for
223+
this style.
224+
225+
While the end result is parsed by Sphinx and renders correctly in a browser, it
226+
isn't the easiest of formats to read. The `NumPy style`_ is a lot nicer to read,
227+
however it consumes a lot more real estate than the previous style::
228+
229+
def random_number_generator(arg1, arg2):
230+
"""
231+
Summary line.
232+
233+
Extended description of function.
234+
235+
Parameters
236+
----------
237+
arg1 : int
238+
Description of arg1
239+
arg2 : str
240+
Description of arg2
241+
242+
Returns
243+
-------
244+
int
245+
Description of return value
246+
247+
"""
248+
return 42
249+
250+
The `sphinx.ext.napoleon`_ plugin allows Sphinx to parse this style of
251+
docstrings, making it easy to incorporate NumPy style docstrings into your
252+
project.
253+
254+
At the end of the day, it doesn't really matter what style is used for writing
255+
docstrings, their purpose is to serve as documentation for anyone who may need
256+
to read or make changes to your code. As long as it is correct, understandable
257+
and gets the relevant points across then it has done the job it was designed to
258+
do.
259+
260+
144261
.. see also:: Further reading on docstrings: :pep:`257`
145262
263+
.. _thomas-cokelaer.info: http://thomas-cokelaer.info/tutorials/sphinx/docstring_python.html
264+
.. _sphinx.ext.napoleon: https://sphinxcontrib-napoleon.readthedocs.io/
265+
.. _`NumPy style`: http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_numpy.html
146266

147267
Other Tools
148268
-----------

0 commit comments

Comments
 (0)