Questions tagged [docstring]

A docstring is a string that occurs as the first statement in a module, function, class, or method definition, and is used to document the object in which it occurs.

A docstring is a string that occurs as the first statement in a module, function, class, or method definition, and is used to document the object in which it occurs.

For example, this Python module:

"""shibboleth.py - answer a common interview question with style and grace."""

DEFAULTS = (
    (3, "Fizz"),
    (5, "Buzz"),
)


def fizzbuzz(limit=15, sep=" ", **kwargs):
    """Print from 1 to `limit`, replacing as in the childhood game."""
    transform = sorted((v, k) for k, v in kwargs.items()) or DEFAULTS
    for number in range(1, limit + 1):
        matches = (word for factor, word in transform if number % factor == 0)
        print(sep.join(matches) or number)

... contains two docstrings: a module-level docstring which contains the filename of the module and a brief description of its purpose, and a function-level docstring which describes the behaviour of the function in which it occurs.

Conventions for writing Python docstrings can be found in PEP 257: Docstring Conventions.

Info on docstrings in other languages than Python can be found on Wikipedia.

745 questions
11
votes
1 answer

How to define a "callable" parameter in a Python docstring?

Consider an implementation of filterNot (basically the opposite of filter): def filterNot(f, sequence): return filter(lambda x: not f(x), sequence) The parameter f can be a "function" or a "method" or a lambda -- or even an object whose class…
Chris W.
  • 1,680
  • 16
  • 35
11
votes
1 answer

How does NumPy process docstrings into sphinx documentation for Parameters?

I want to build our documentation using sphinx and get the same formatting on parameters as the NumPy docs ( https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt ) I have found two ways to document parameters in this rst style with…
jkeesh
  • 3,289
  • 3
  • 29
  • 42
10
votes
2 answers

How to document kwargs according to Numpy style docstring?

So, I've found posts related to other styles and I am aware of this NumPy page about the documentation but I am confused. I didn't understand how to add each kwargs to the parameters section of a method. This is from the given web page: def…
MehmedB
  • 1,059
  • 1
  • 16
  • 42
10
votes
2 answers

How to specify multiple return types in a function docstring in Python?

I am aware of the syntax used to build a docstring for Google style such as: def function_with_types_in_docstring(param1, param2): """Example function with types documented in the docstring. `PEP 484`_ type annotations are supported. If…
10
votes
2 answers

Make 'Google' the default docstring style for ALL projects PyCharm

I'm aware that I can go to Settings | Tools | Python Integrated Tools | Docstrings | Docstring format | Google, but this only sets the the docstring format for the current project (as indicated by the heading 'for current project' in the window).…
Mattwmaster58
  • 2,266
  • 3
  • 23
  • 36
10
votes
2 answers

How can I make Python/Sphinx document object attributes only declared in __init__?

I have Python classes with object attributes which are only declared as part of running the constructor, like so: class Foo(object): def __init__(self, base): self.basepath = base temp = [] for run in…
andybuckley
  • 1,114
  • 2
  • 11
  • 24
10
votes
1 answer

What's meaning of these formats in twisted's docstring?

In twisted's sourcecode, many docstrings contain formats like this: L{xxx} or C{xxx} or a line begin with an '@', what's their meanings? for example, in twisted/internet/interfaces.py: def registerProducer(producer, streaming): """ Register…
Coaku
  • 977
  • 1
  • 9
  • 23
10
votes
4 answers

Automated way to switch from epydoc's docstring formatting to sphinx docstring formatting?

I've got a project which I documented using epydoc. Now I'm trying to switch to sphinx. I formatted all my docstrings for epydocs, using B{}, L{} etc for bolding, linking and the like, and using @param, @return, @raise etc to explain input, output,…
Niek de Klein
  • 8,524
  • 20
  • 72
  • 143
9
votes
2 answers

Python DocString (Google Style): How to document class attributes?

I have this small example: """Sandbox module""" class Toto: """ This class is an example Attributes: class_attribute (str): The class attribute #Unresolved reference instance_attribute (str): The instance attribute #OK …
Milan
  • 1,547
  • 1
  • 24
  • 47
9
votes
1 answer

Python "See help(type(self)) for accurate signature."

I have seen the following statement in a number of docstrings when help()ing a class: "See help(type(self)) for accurate signature." Notably, it is in the help() for scipy.stats.binom.__init__ and for stockfish.Stockfish.__init__ at the very least. …
Him
  • 5,257
  • 3
  • 26
  • 83
9
votes
1 answer

How can I parse a numpydoc docstring and access components?

I'd like to parse a numpydoc docstring and access each component programatically. For example: def foobar(a, b): '''Something something Parameters ---------- a : int, default: 5 Does something cool b : str …
trianta2
  • 3,952
  • 5
  • 36
  • 52
9
votes
1 answer

How does one specify a type for variables in Python docstrings for Sphinx?

You can specify types of parameters in Python docstrings like this: def __init__(self, canvas, segments): """Class constructor. :param canvas: the PDF canvas object :param segment: The layer segments to be drawn. :type canvas:…
Torsten Bronger
  • 9,899
  • 7
  • 34
  • 41
9
votes
4 answers

Repetitive content in docstrings

What are good ways to deal with repetitive content in docstrings? I have many functions that take 'standard' arguments, which have to be explained in the docstring, but it would be nice to write the relevant parts of the docstring only once, as this…
astrofrog
  • 32,883
  • 32
  • 90
  • 131
9
votes
3 answers

How would I pretty print a python docstring?

I have a python file that has raw strings as the docstrings. def a(): '\n\tthis\n\tis\n\tthe docstring.\n\t' print 'hello world' how would I rewrite the docstring to look like def a(): """ this is the docstring. """ …
baallezx
  • 471
  • 3
  • 14
9
votes
1 answer

Sphinx autodoc functions within module

I am just getting started with sphinx and willing to learn. I would like to break up my various functions into different sections within my index.rst file. So each function has it's own header. So for example if I have a python file named test.py…
Ken Carrier
  • 91
  • 1
  • 1
  • 2