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

How can I programmatically access DocStrings?

I am writing a macro, that takes a function name, and declares a couple of other version of the function. I want to give these variations the same doc string as the original method, with perhaps a few changes. To do this, I need to retrieve the…
Frames Catherine White
  • 27,368
  • 21
  • 87
  • 137
6
votes
1 answer

How should multiline protocol method docstrings be formatted?

Multiline function or protocol docstrings can be easily formatted: (defn foo "Does a very complicated thing that I need to explain in excruciating detail. Firstly, this function stringifies x with the standard greeting of 'Hello'. Secondly, it…
Sam Estep
  • 12,974
  • 2
  • 37
  • 75
6
votes
2 answers

How do I automatically link to a parameter type in ReST docstrings in Sphinx?

For example, I have the following code: # Solve for coefficients of quadratic approximation def quad(p, x): """Solves for the coefficients of the quadratic approximation of a polynomial ``p`` at points ``x``. :param…
Kit
  • 30,365
  • 39
  • 105
  • 149
6
votes
1 answer

Have Sphinx replace docstring text

I am documenting code in Sphinx that resembles this: class ParentClass(object): def __init__(self): pass def generic_fun(self): """Call this function using /run/ParentClass/generic_fun()""" do_stuff() class…
Charles L.
  • 5,795
  • 10
  • 40
  • 60
5
votes
1 answer

epydoc hide some class functions?

I have some methods in my class which are only meant to be used by other methods of the class. I've prefixed their names with '_'. Can I hide those functions from epydoc? Is it a good idea? Should I use '_' or double underscore? To be honest I…
user975135
5
votes
1 answer

Sphinx remove dataclass fields from autodoc

As this post describes, autodoc will eagerly add the class variables to the documentation even if napoleon adds the fields as documentation in: from dataclasses import dataclass @dataclass class Foo(): """Some class Attributes: a:…
OneRaynyDay
  • 3,658
  • 2
  • 23
  • 56
5
votes
1 answer

python automatically adding type hints in docstring

I'm looking for a tool that adds the type annotation that are already added in a function to the docstring generated by PyCharm. I have been following this issue on JetBrains for a long time and no progress seem to be made on this ticket yet. I saw…
5
votes
2 answers

Copying the docstring of function onto another function by name

I'm looking to copy the docstring of a function in the same file by name (with a decorator). I can easily do it with a function that is out of the current module, but I'm a bit confused when it comes to the same module (or the same class more…
Iced Chai
  • 472
  • 2
  • 13
5
votes
1 answer

Writing double backslash in python Docstring

I am writing documentations for a python package with a clear_stop_character function, in which users can provide extra stop-chars in a list. In the documentation I have written: """ stoplist (list, default empty): Accepts a list of extra stop…
maaniB
  • 595
  • 6
  • 23
5
votes
1 answer

How to write docstring in Python for a function that returns None but write files in disk?

I want to use Python docstring to better document my code. In the function below, it takes a list as input and write a file into disk. I want to know how to document the output of function in docstring as the function return type in None. Does it…
masoud
  • 535
  • 3
  • 16
5
votes
1 answer

Why do definitions have a space before the colon in NumPy docstring sections?

Numpy docstring guide says: The colon must be preceded by a space, or omitted if the type is absent. and gives an example: Parameters ---------- x : type Description of parameter `x`. y Description of parameter `y` (with type not…
hans
  • 1,043
  • 12
  • 33
5
votes
2 answers

Equivalent of Python's help() in javascript or nodejs

When I want to know the number of arguments and argument types of a function in python, i just use the function help() to get them. But in javascript or nodejs, it is really hard to know the argument type and number of arguments of a function. Is…
madhan01
  • 117
  • 5
5
votes
1 answer

"Docstrings" in Racket?

I know Racket doesn't have "docstrings" in the same way that many other languages do, but given how convenient documenting things at the source is, I'd like to approximate something like it in Racket. When I first learned about Scribble and #langs,…
mindthief
  • 12,755
  • 14
  • 57
  • 61
5
votes
1 answer

Where to put the doc string for a decorator

I'm trying to document a decorator, but am not sure where the doc string should go. Technically, it's the inner wrapper that contains the parameters I want to document, but a user is going to be applying the outer function name as the…
Chris
  • 1,313
  • 2
  • 13
  • 26
5
votes
3 answers

Can you pass a None argument when docstring requires a string?

Ok, this question has probably been answered somewhere but my Google-fu hasn't found the right combination of keywords yet. I have a function that accepts a string, but when I pass None, Pycharm's inspection is flagging a type error. Is this an…
jon_two
  • 1,073
  • 1
  • 23
  • 34