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
9
votes
3 answers

Python 3 - Method docstring inheritance without breaking decorators or violating DRY

This question seems to come up regularly both on StackOverflow and elsewhere, yet I wasn't able to find a completely satisfactory solution anywhere. There seem to be two types of common solutions. The first one (from e.g.…
Nikratio
  • 2,338
  • 2
  • 29
  • 43
9
votes
1 answer

How do I get Sphinx to ignore the GPL notice in my docstring?

I'm using Sphinx to document my Python package. When I use the automodule directive on my module: .. automodule:: mymodule :members: It prints everything including the GPL notice in the docstring. Is there any way to tell Sphinx to ignore the…
tompreston
  • 630
  • 7
  • 24
8
votes
3 answers

Tool to compare function signature to docstring

Is there a tool that can check if the arguments listed in the docstring match the signature of the function call? It should be able to deal with numpy-style docstrings. I am regularly using R CMD CHECK, which finds documentation/code mismatches in R…
8
votes
1 answer

Python: how to embed all docstring help at package level help menu?

What I mean to ask is: TLDR: how do I have my package's help include all underlying docstrings? I have created a package. That package has all the proper __init__.py files and all the proper docstrings (module, function, class, and method level…
Mike Williamson
  • 4,915
  • 14
  • 67
  • 104
8
votes
0 answers

Add dictionary keys in docstring sphinx

What is the recommended way of adding a docstring for the keys of a dictionary? I am using python 2.7 and Sphinx. For instance, in the following code, how should I mention the keys 'a' and 'b' for my_dict? (But maybe it not necessary to go in such…
RomB
  • 295
  • 3
  • 17
8
votes
2 answers

How do I change Emacs's font face for Python docstrings?

I'm just starting to learn Python and use Emacs as my editor. Currently, Emacs uses the same color for normal strings (single quotes) and docstrings (triple quotes). I want docstrings to be a different color, so I used the 'Options->Customize…
Jesse DeGuire
  • 359
  • 3
  • 7
8
votes
1 answer

What is the best way in python to write docstrings for lambda functions?

I usually comment my functions using multi-line docstrings with """, as mentioned in : https://www.python.org/dev/peps/pep-0257/ def func1(x): """ This function does ... """ ... But what is the best way to comment a lambda function ? I…
Eric H.
  • 2,152
  • 4
  • 22
  • 34
8
votes
1 answer

PyCharm: How to document :rtype: for function that returns generator

I try to document a :rtype: docstring parameter returning a generator of Node: def __iter__(self): """iterate over node children :rtype: ??? """ for node in self.children.itervalues(): yield node What :rtype: is supposed to…
Narann
  • 819
  • 2
  • 8
  • 20
8
votes
1 answer

Is it possible to bulk-add docstrings to all the functions in PyCharm?

I have a python project that many of the functions and classes missing docstrings. I know that PyCharm can automatically add docstrings by the Insert documentation string stub command using intention action. However, for large number of methods and…
Mohammad ElNesr
  • 2,477
  • 4
  • 27
  • 44
8
votes
1 answer

Make function have ellipsis for arguments in help() function

If you type help(vars), the following is produced: vars(...) vars([object]) -> dictionary Without arguments, equivalent to locals(). With an argument, equivalent to object.__dict__. When I do the following: def func(x, y):…
zondo
  • 19,901
  • 8
  • 44
  • 83
8
votes
1 answer

csv-table formatting in Python docstrings (Sphinx) - multiple lines in one cell

I'm using Sphinx to document a Python project. There seems to be a bit of inconsistency with the .. csv-table:: directive. The main issue is a new line in a cell. And my questionable mental health. The following code: .. csv-table:: :header:…
Derorrist
  • 2,753
  • 1
  • 17
  • 26
8
votes
2 answers

Tell PyCharm code generated fields of class

As a minimal case I have a class Example that works like in abstract capacity for a range of other classes. class Example(object): def __init__(self, **kwargs): for key, value in kwargs.items(): setattr(self, key, value) …
deinonychusaur
  • 7,094
  • 3
  • 30
  • 44
8
votes
1 answer

Is there a Groovy equivilent to Python Docstrings?

In Python, when commenting a function, you can do it in a way that makes it easier for documentation to be automatically generated. They refer to it as a docstring. Now that I've made an abstract class in Groovy that I'd like to pass around, is…
techHenson
  • 93
  • 1
  • 5
8
votes
2 answers

Difference between ":", "@" and nothing in python docstrings

I'm just trying to get a better feel for the layout of Python docstrings (Between the """ """) I've seen docstrings with different layouts...such as... """ @DESCRIPTION Ive seen tags STARTING with an at-sign :DESCRIPTION: Tags with…
RightmireM
  • 2,381
  • 2
  • 24
  • 42
8
votes
1 answer

Can I view the doc string of a function in Python using VIM?

Is there any way to view a function's doc string when writing Python in VIM? For instance: def MyFunction(spam): """A function that foobars the spam returns eggs""" return foobar(spam).eggs() I'd like to be able to type…
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319