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
37
votes
2 answers

reStructuredText in Sphinx and Docstrings: single vs. double back-quotes or back-ticks difference

From the documentation, it appears that the double back-quote is used for literals, while the single back-quote is used when there is code text to be intepreted. This would lead me to to write the docstring for method f() below: class A(B): …
Jeet
  • 38,594
  • 7
  • 49
  • 56
35
votes
4 answers

What's the difference on docstrings with triple SINGLE quotes and triple DOUBLE quotes?

I was just wondering what is the difference between two ways of writing Python Docstrings (__doc__): three single quotes: ''' Comment goes here ''' three double quotes: """ Comment goes here """ Is there any subtle difference in the way doc…
prashu
  • 689
  • 2
  • 7
  • 10
34
votes
1 answer

PEP 257 docstring trim in standard library?

PEP 257 says: Docstring processing tools will strip a uniform amount of indentation from the second and further lines of the docstring, equal to the minimum indentation of all non-blank lines after the first line. Any indentation in the first line…
BrenBarn
  • 242,874
  • 37
  • 412
  • 384
31
votes
7 answers

How to write meaningful docstrings?

What, in Your opinion is a meaningful docstring? What do You expect to be described there? For example, consider this Python class's __init__: def __init__(self, name, value, displayName=None, matchingRule="strict"): """ name - field name …
Konrads
  • 2,206
  • 2
  • 30
  • 45
31
votes
4 answers

Triple-double quote v.s. Double quote

What is the preferred way to write Python doc string? """ or " In the book Dive Into Python, the author provides the following example: def buildConnectionString(params): """Build a connection string from a dictionary of parameters. …
Mingyu
  • 31,751
  • 14
  • 55
  • 60
29
votes
5 answers

How to auto-generate the type of a field in a docstring in PyCharm?

When I create a function with parameters, PyCharm offers me to create the docstring with :param param_name: field, which is pretty good. But I also need to add the :type param_name:. So from that : def foo(bar, xyz): return bar + xyz With the…
FunkySayu
  • 7,641
  • 10
  • 38
  • 61
29
votes
2 answers

PyCharm docstrings linking to classes

IntelliJ IDEA allows for linking to other methods in Java document comments. This allows me to move the cursor over a symbol and jump to the definition using a keyboard shortcut, as well as holding down ctrl while hovering with the mouse displaying…
gak
  • 32,061
  • 28
  • 119
  • 154
29
votes
3 answers

function name + tab does not return docstring in IPython

In IPython, I am used to write function( and then strike a tab, and get the contents of the docstring and a list of the named arguments. However, this stopped working since I installed IPython 2.0. Is there an explanation or a know fix?
dmvianna
  • 15,088
  • 18
  • 77
  • 106
29
votes
5 answers

How should unit tests be documented?

I'm trying to improve the number and quality of tests in my Python projects. One of the the difficulties I've encountered as the number of tests increase is knowing what each test does and how it's supposed to help spot problems. I know that part of…
ddbeck
  • 3,855
  • 2
  • 28
  • 22
29
votes
2 answers

Clojure docstring for libraries/namespaces

How to add docstrings and/or comments to Clojure libaries/namespaces as a whole, i.e. not just to specific functions within the namespace? I've noticed that the clojure source uses (comment ...) in some places to do this (example), is that…
mikera
  • 105,238
  • 25
  • 256
  • 415
28
votes
3 answers

Multiline Clojure docstrings

I've noticed that Clojure multiline docstrings seem to be manually formatted in most cases, including the ones in clojure.core. Example from https://github.com/clojure/clojure/blob/master/src/clj/clojure/core.clj : (defn flatten "Takes any…
mikera
  • 105,238
  • 25
  • 256
  • 415
27
votes
1 answer

docstring in class or __init__ constructor?

There seem to be two places where you can put docstrings for a class: Right under the class definition: class MyClass: """Summary of MyClass. Body. """ Right under the __init__ constructor: class MyClass: def __init__(self, arg1,…
Jin
  • 778
  • 1
  • 8
  • 23
27
votes
2 answers

Which is more efficient: Python docstrings or type-hints?

I want to add some support for auto-completion to my Python code with Jedi. This can be done by using either function docstrings or type hints (or both). def function_with_types_in_docstring(param1, param2): """Example function with types…
planetp
  • 14,248
  • 20
  • 86
  • 160
26
votes
7 answers

Why is imperative mood important for docstrings?

The error code D401 for pydocstyle reads: First line should be in imperative mood. I often run into cases where I write a docstring, have this error thrown by my linter, and rewrite it -- but the two docstrings are semantically identical. Why is it…
Richard
  • 459
  • 1
  • 5
  • 8
26
votes
2 answers

How to specify different return types in python docstring

I'm currently writing documentation for my python package using Sphinx and the autodoc plugin. For a function return value I can e.g. write :returns: int: count which tells sphinx that there is a return value of type int, named count. I now got a…
Igl3
  • 4,900
  • 5
  • 35
  • 69
1 2
3
49 50