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
3
votes
1 answer

In the Raises section of the docstring of a function, should the exceptions raised indirectly be listed too?

For example, for: def foo(a): if a == 10: raise FooError return 1 / a Should the docstring for foo contain something like: """Raises: FooError: if a is equal to 10. ZeroDivisionError: if a is equal to 0. """ Or should it…
LoneCodeRanger
  • 413
  • 6
  • 18
3
votes
1 answer

How to indicate a valid range in a Python docstring using Sphinx?

Is there a way to indicate a "valid range" in a Python docstring using Sphinx? For example, consider the following linear function. def f(m, x, b): """ Returns the `y` value of a linear function using slope-intercept form. :param…
MikeyE
  • 1,756
  • 1
  • 18
  • 37
3
votes
2 answers

How to remove static class variable using automodule with Sphinx?

I'm currently using Sphinx (first time using it) to build the docs for my modules and I have some classes which have some class variables that are initialized with a default value. For ex: class ConfigSettings(object): """Class that manages a…
DarkImage
  • 63
  • 1
  • 8
3
votes
1 answer

Dynamic docstring for a Python endpoint

I have a Django-REST-framework class based view/endpoint that looks like that: from rest_framework.generics import RetrieveUpdateAPIView from .serializers import ViewSerializer INPUT_FORMAT = { "input1": [9, 23, 55], "input2": "string", …
bolino
  • 867
  • 1
  • 10
  • 27
3
votes
1 answer

Is it possible to include the value of a constant in a docstring?

I would like to do this: FOO_CONSTANT = 1 def foo(): """asdf """ + str(FOO_CONSTANT) print(foo.__doc__) Which should print: asdf 1 But instead it prints None I can get the expected output with FOO_CONSTANT = 1 def foo(): """asdf…
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
3
votes
1 answer

Printing python docstrings for all Class methods

I would like to print the documentation for a module to include docstrings for each class and each method within the class, but only including methods I have written; not default methods. This is my_module.py: import json import pandas as pd class…
doctorer
  • 1,672
  • 5
  • 27
  • 50
3
votes
0 answers

Dynamically overriding Python docstring

I'd like to dynamically generate docstrings for a CRUD REST API I'm building. I make heavy use Flask's MethodView, and as the docstrings are very similar I thought I'd create a mixin that would dynamically generate this documentation for me. The…
CadentOrange
  • 3,263
  • 1
  • 34
  • 52
3
votes
1 answer

How to add code snippets to python docstring (not as doctest)?

I would like to provide code snippets to show-case how one could use a particular method or class in python. How can I do that? In Java one could use
 ... 
to do so. Doctest is the only way? As I look at the existing docstrings for…
Azim
  • 1,596
  • 18
  • 34
3
votes
0 answers

How to add python docstring for nested dictionary return type

I'm trying to create a docstring for a return type Dictionary. The dictionary has Date->Dictionary key-value pairs. The nested Dictionary has 2 string->int key-value pairs and a string->Dictionary key-value pair Current docstring looks like the…
3
votes
1 answer

How to preserve a docstring of a decorated class for sphinx documentation?

I have a decorator that has a nested definition of the wrapping class. Wrapper maintains as the attribute the original class that it wraps. Toy example looks like this: def decorator(cls): class Wrapper(object): original = cls …
emerte
  • 58
  • 5
3
votes
2 answers

How to get just summary line from python docstring?

I write test functions. In the docstring, I usually mention test case name as the summary line. The test case description follows that. Now I just need to fetch the test case name (first line) from docstring. Is there any pythonic way to do it? …
XgigPro
  • 134
  • 1
  • 11
3
votes
2 answers

Using typing module in docstring

Suppose I have a function with a docstring where I declare the return type as a tuple with two strings: def foo(): """ Returns: Tuple[str, str]: Tuple of first name and last name """ Am I supposed to import Tuple from typing if…
damd
  • 6,116
  • 7
  • 48
  • 77
3
votes
2 answers

sphinx and autodocstring from VScode with python code

I'm collaborating on a project with people that use vscode. We write Python code. I asked them to generate the docstrings for their functions, and they used the Autodocstring from vscode. Here is a docstring they came up with: """ Subclass ObjectCAD…
JPFrancoia
  • 4,866
  • 10
  • 43
  • 73
3
votes
0 answers

Python Sphinx docstrings - reference parameter block multiple times

I've got multiple classes that all have a base they inherit from. The __init__ has a ton of parameters for each that are all basically the same. Is there anyway in the docstrings with sphinx to just write the param definition once, then reference…
Adam Haile
  • 30,705
  • 58
  • 191
  • 286
3
votes
1 answer

How to specify return type and describe the return value in one-line ReST docstring?

I know I can abbreviate the following ReST docstring """ :type flag: bool :param flag: new value for the flag """ as """ :param bool flag: new value for the flag """ Can I abbreviate in a similar manner the following? """ :rtype: bool :returns:…
user7610
  • 25,267
  • 15
  • 124
  • 150