4 Readability is a primary focus for Python developers, in both project
5 and code documentation. Following some simple best practices can save
6 both you and others a lot of time.
11 A ``README`` file at the root directory should give general
12 information to the users and the maintainers. It should be raw text or
13 written in some very easy to read markup, such as
14 :ref:`reStructuredText-ref` and Markdown. It should contain a few
15 lines explaining the purpose of the project or the library (without
16 assuming the user knows anything about the project), the url of the
17 main source for the software, and some basic credit information. This
18 file is the main entry point for readers of the code.
20 An ``INSTALL`` file is less necessary with python. The installation
21 instructions are often reduced to one command, such as ``pip install
22 module`` or ``python setup.py install`` and added to the ``README``
25 A ``LICENSE`` file should *always* be present and specify the license under which the
26 software is made available to the public.
28 A ``TODO`` file or a ``TODO`` section in ``README`` should list the
29 planned development for the code.
31 A ``CHANGELOG`` file or section in ``README`` should compile a short
32 overview of the changes in the code base for the latest versions.
37 Depending on the project, your documentation might include some or all
38 of the following components:
40 - A *introduction* should show a very short overview of what can be
41 done with the product, using one or two extremely simplified use
42 cases. This is the thirty-second pitch for your project.
44 - A *tutorial* should show some primary use cases in more detail. The reader will
45 follow a step-by-step procedure to set-up a working prototype.
47 - An *API reference* is typically generated from the code (see
48 :ref:`docstrings <docstring-ref>`). It will list all publicly available interfaces,
49 parameters, and return values.
51 - *Developer documentation* is intended for potential contributors. This can
52 include code convention and general design strategy of the project.
59 Sphinx_ is far and away the most popular python documentation
60 tool. **Use it.** It converts :ref:`restructuredtext-ref` markup language
61 into a range of output formats including HTML, LaTeX (for printable
62 PDF versions), manual pages, and plain text.
64 There is also **great**, **free** hosting for your Sphinx_ docs:
65 `Read The Docs`_. Use it. You can configure it with commit hooks to
66 your source repository so that rebuilding your documentation will
71 Sphinx is famous for its API generation, but it also works well
72 for general project documentation. This Guide is built with
73 Sphinx_ and is hosted on `Read The Docs`_
75 .. _Sphinx: http://sphinx.pocoo.org
76 .. _Read The Docs: http://readthedocs.org
78 .. _restructuredtext-ref:
83 Most Python documentation is written with reStructuredText_. It's like
84 Markdown with all the optional extensions built in.
86 The `reStructuredText Primer`_ and the `reStructuredText Quick
87 Reference`_ should help you familiarize yourself with its syntax.
89 .. _reStructuredText: http://docutils.sourceforge.net/rst.html
90 .. _reStructuredText Primer: http://sphinx.pocoo.org/rest.html
91 .. _reStructuredText Quick Reference: http://docutils.sourceforge.net/docs/user/rst/quickref.html
94 Code Documentation Advice
95 -------------------------
97 Comments clarify code and begin with a hash (``#``).
101 In Python, *docstrings* describe modules, classes, and functions: ::
103 def square_and_rooter(x):
104 """Returns the square root of self times self."""
107 In general, follow the `comment section of PEP 0008`_ (the "Python Style Guide").
109 .. _comment section of PEP 0008: http://www.python.org/dev/peps/pep-0008/#comments
111 Commenting Sections of Code
112 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
114 *Do not use triple-quote strings to comment code*. This is not a good
115 practice, because line-oriented command-line tools such as grep will
116 not be aware that the commented code is inactive. It is better to add
117 hashes at the proper indentation level for every commented line. Your
118 editor probably has the ability to do this easily, and it is worth
119 learning the comment/uncomment toggle. (*e.g.* ctrl-v on Vim)
124 Some tools use docstrings to embed more-than-documentation behavior,
125 such as unit test logic. Those can be nice, but you won't ever go
126 wrong with vanilla "here's what this does."
128 Docstrings versus Block comments
129 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
131 These aren't interchangeable. For a function or class, the leading
132 comment block is a programmer's note. The docstring describes the
133 operation of the function or class: ::
135 # This function slows down program execution for some reason.
136 def square_and_rooter(x):
137 """Returns the square root of self times self."""
140 .. seealso:: Further reading on docstrings: `PEP 0257`_
142 .. _PEP 0257: http://www.python.org/dev/peps/pep-0257/
148 You might see these in the wild. Use :ref:`sphinx-ref`.
151 Pycco is a "literate-programming-style documentation generator"
152 and is a port of the node.js Docco_. It makes code into a
153 side-by-side HTML code and documentation.
155 .. _Pycco: http://fitzgen.github.com/pycco
156 .. _Docco: http://jashkenas.github.com/docco
159 Ronn builds unix manuals. It converts human readable textfiles to roff for terminal display, and also to HTML for the web.
161 .. _Ronn: https://github.com/rtomayko/ronn
164 Epydoc is discontinued. Use :ref:`sphinx-ref` instead.
166 .. _Epydoc: http://epydoc.sourceforge.net