Professional Documents
Culture Documents
PEP257
PEP257
Abstract
========
Rationale
=========
If you violate these conventions, the worst you'll get is some dirty
looks. But some software (such as the Docutils_ docstring processing
system [1]_ [2]_) will be aware of the conventions, so following them
will get you the best results.
Specification
=============
What is a Docstring?
--------------------
All modules should normally have docstrings, and all functions and
classes exported by a module should also have docstrings. Public
methods (including the ``__init__`` constructor) should also have
docstrings. A package may be documented in the module docstring of
the ``__init__.py`` file in the package directory.
One-line Docstrings
--------------------
One-liners are for really obvious cases. They should really fit on
one line. For example::
def kos_root():
"""Return the pathname of the KOS root directory."""
global _kos_root
if _kos_root: return _kos_root
...
Notes:
- Triple quotes are used even though the string fits on one line.
This makes it easy to later expand it.
- The closing quotes are on the same line as the opening quotes. This
looks better for one-liners.
Multi-line Docstrings
----------------------
The docstring for a class should summarize its behavior and list the
public methods and instance variables. If the class is intended to be
subclassed, and has an additional interface for subclasses, this
interface should be listed separately (in the docstring). The class
constructor should be documented in the docstring for its ``__init__``
method. Individual methods should be documented by their own
docstring.
Keyword arguments:
real -- the real part (default 0.0)
imag -- the imaginary part (default 0.0)
"""
if imag == 0.0 and real == 0.0:
return complex_zero
...
Unless the entire docstring fits on a line, place the closing quotes
on a line by themselves. This way, Emacs' ``fill-paragraph`` command
can be used on it.
def trim(docstring):
if not docstring:
return ''
# Convert tabs to spaces (following the normal Python rules)
# and split into a list of lines:
lines = docstring.expandtabs().splitlines()
# Determine minimum indentation (first line doesn't count):
indent = sys.maxint
for line in lines[1:]:
stripped = line.lstrip()
if stripped:
indent = min(indent, len(line) - len(stripped))
# Remove indentation (first line is special):
trimmed = [lines[0].strip()]
if indent < sys.maxint:
for line in lines[1:]:
trimmed.append(line[indent:].rstrip())
# Strip off trailing and leading blank lines:
while trimmed and not trimmed[-1]:
trimmed.pop()
while trimmed and not trimmed[0]:
trimmed.pop(0)
# Return a single string:
return '\n'.join(trimmed)
def foo():
"""
This is the second line of the docstring.
"""
To illustrate::
def foo():
"""A multi-line
docstring.
"""
def bar():
"""
A multi-line
docstring.
"""
.. _Docutils: http://docutils.sourceforge.net/
.. _Doc-SIG: http://www.python.org/sigs/doc-sig/
Copyright
=========
Acknowledgements
================
The "Specification" text comes mostly verbatim from the `Python Style
Guide`_ essay by Guido van Rossum.
This document borrows ideas from the archives of the Python Doc-SIG_.
Thanks to all members past and present.
..
Local Variables:
mode: indented-text
indent-tabs-mode: nil
fill-column: 70
sentence-end-double-space: t
End: