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
20
votes
4 answers

Is there an option to print the output of help()?

Is there an option to print the output of help('myfun'). The behaviour I'm seeing is that output is printed to std.out and the script waits for user input (i.e. type 'q' to continue). There must be a setting to set this to just dump…
mathtick
  • 6,487
  • 13
  • 56
  • 101
19
votes
2 answers

Python Visual Studio Code autoDocstring Configuration

Goal: generate docstring in vscode for Python automatically and format the generated docstring to my liking. Solution: I installed the autoDocstring extension. Problem: I don't know how to get the generated docstring to be formatted the way I want…
syth3
  • 555
  • 1
  • 4
  • 13
19
votes
2 answers

Commenting JavaScript functions á la Python Docstrings

It is valid JavaScript to write something like this: function example(x) { "Here is a short doc what I do."; // code of the function } The string actually does nothing. Is there any reason, why one shouldn't comment his/her functions in…
Boldewyn
  • 81,211
  • 44
  • 156
  • 212
17
votes
2 answers

Can a Python docstring be calculated (f-string or %-expression)?

Is it possible to have a Python docstring calculated? I have a lot of repetitive things in my docstrings, so I'd like to either use f-strings or a %-style format expression. When I use an f-string at the place of a docstring importing the module…
17
votes
3 answers

How can I resolve pydocstyle error "D205: 1 blank line required between summary line and description (found 0)"?

I'm trying to use pydocstyle to check the quality of my docstring and I'm getting this error: D205: 1 blank line required between summary line and description (found 0) This is how my code looks like def func(input): """ Function that does…
Richard Ackon
  • 651
  • 1
  • 7
  • 11
17
votes
2 answers

How do I document :rtype: for a function that returns multiple possible data types?

In a Python docstring how should one document the :rtype: for a function that can return multiple possible data types? For example, if a function can return defaultdict OR dict OR list, based on the functions parameters, how do you document…
stack_lech
  • 990
  • 2
  • 10
  • 28
17
votes
1 answer

What is the standard way for writing default values in a python docstring?

I have a function with parameters set to default values. I'm using a NumPy-style docstring, but I've seen the default values written elsewhere. What is the commonly accepted placement for writing "default" in the docstring? def…
pylang
  • 40,867
  • 14
  • 129
  • 121
16
votes
3 answers

How to show docstring or function signature in VS Code for a Jupyter Notebook?

I currently have installed VS Code as an alternative editor to Jupyterlab for Python Data Science development. Now I am wondering about how to show the docstring or signature of a function? I didn't found anything about a shortcut oder a needed…
Flo
  • 233
  • 2
  • 6
16
votes
2 answers

How can I produce a numpy-like documentation?

I'm working a lot with spyder and the object inspector, which I find pretty convenient as an instant help function. Some modules seem to profit very nicely from this function. For example a pretty basic numpy function (numpy.absolute) produces the…
Dschoni
  • 3,714
  • 6
  • 45
  • 80
16
votes
5 answers

Are Python docstrings and comments stored in memory when a module is loaded?

Are Python docstrings and comments stored in memory when a module is loaded? I've wondered if this is true, because I usually document my code well; may this affect memory usage? Usually every Python object has a __doc__ method. Are those…
Kenny Meyer
  • 7,849
  • 6
  • 45
  • 66
15
votes
4 answers

In emacs Python mode, how do I set a different auto-fill width for docstrings and code?

I would like to have auto-fill set to 79 columns for code sections and 72 for docstrings to get automatic PEP8 compliance. There seems to be an option to do this for Lisp mode (emacs-lisp-docstring-fill-column) but not for Python. Is there an…
Tim D
  • 1,645
  • 1
  • 25
  • 46
15
votes
1 answer

Docstring tag for 'yield' keyword

There are some tags for docstrings in python, like @param and @return, for example: def my_method(a_param): ''' @param a_param: Description of this param @return: The return value of the method ''' return int(a_param) * (other or…
alfonso.kim
  • 2,844
  • 4
  • 32
  • 37
15
votes
1 answer

How to docstring kwargs and their expected types

What is the conventional way to express in a docstring the expected types of keyword arguments? Or is this out of principle something I should not be doing at all? Google is suspiciously un-forthcoming on the subject. (I am interested in doing this…
levraininjaneer
  • 1,157
  • 2
  • 18
  • 38
15
votes
1 answer

How do I insert highlight or code-block into Sphinx-style docstrings?

For example: def foo(): ''' .. highlight:: python import sys ''' Doesn't produce desired output (it prints the word "highlight" verbatim and doesn't format the following code in any special way). Same happens for code-block. I…
wvxvw
  • 8,089
  • 10
  • 32
  • 61
15
votes
2 answers

Simple way to convert Python docstrings from reStructured Text to Google style?

Does anyone now a simple way do convert all docstrings in an existing project from reStructured Text to the Google format? It looks like Napoleon can do something like that, but it looks very complicated, so I figured I'd ask if someone has done…
gmolau
  • 2,815
  • 1
  • 22
  • 45