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

print(__doc__) in Python 3 script

I can't figure out what does the print(__doc__) do at the beginning of a script, like in this Scikit example. I have been looking for Python docstrings in google, and it seems __doc__ is useful to provide some documentation in, say, functions. But I…
Tanguy
  • 3,124
  • 4
  • 21
  • 29
72
votes
4 answers

How to specify that a parameter is a list of specific objects in Python docstrings

I really like using docstrings in Python to specify type parameters when projects get beyond a certain size. I'm having trouble finding a standard to use to specify that a parameter is a list of specific objects, e.g. in Haskell types I'd use…
Alex
  • 18,332
  • 10
  • 49
  • 53
66
votes
6 answers

How do I programmatically set the docstring?

I have a wrapper function that returns a function. Is there a way to programmatically set the docstring of the returned function? If I could write to __doc__ I'd do the following: def wrapper(a): def add_something(b): return a + b …
oceanhug
  • 1,352
  • 1
  • 11
  • 24
65
votes
4 answers

Python decorator handling docstrings

I have a problem using docstrings with decorators. Given the following example: def decorator(f): def _decorator(): print 'decorator active' f() return _decorator @decorator def foo(): '''the magic foo function''' …
Günther Jena
  • 3,706
  • 3
  • 34
  • 49
63
votes
3 answers

How do I document a constructor for a class using Python dataclasses?

I have some existing Python 3.6 code that I'd like to move to Python 3.7 dataclasses. I have __init__ methods with nice docstring documentation, specifying the attributes the constructors take and their types. However, if I change these classes to…
anahata
  • 826
  • 1
  • 7
  • 14
58
votes
6 answers

Inheriting methods' docstrings in Python

I have an OO hierarchy with docstrings that take as much maintenance as the code itself. E.g., class Swallow(object): def airspeed(self): """Returns the airspeed (unladen)""" raise NotImplementedError class…
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
58
votes
7 answers

Are there any real alternatives to reStructuredText for Python documentation?

I'm starting an open source Python project shortly and I'm trying to decide in advance how to write my docstrings. The obvious answer would be using reStructuredText and Sphinx with autodoc, because I really like the idea of simply properly…
Hubro
  • 56,214
  • 69
  • 228
  • 381
57
votes
1 answer

How to express multiple types for a single parameter or a return value in docstrings that are processed by Sphinx?

Sometimes a function in Python may accept an argument of a flexible type. Or it may return a value of a flexible type. Now I can't remember a good example of such a function right now, therefore I am demonstrating what such a function may look like…
Lone Learner
  • 18,088
  • 20
  • 102
  • 200
49
votes
1 answer

Documenting `tuple` return type in a function docstring for PyCharm type hinting

How can I document that a function returns a tuple in such a way that PyCharm will be able to use it for type hinting? Contrived example: def fetch_abbrev_customer_info(customer_id): """Pulls abbreviated customer data from the database for the…
user212218
48
votes
4 answers

What is the Python docstring format supported by Visual Studio Code?

How does VS Code interpret markup/markdown and layout in Python docstrings on mouse hover? There are several issues reported for this display but there doesn't seem to exist any official info on what the current format is.
AdSR
  • 625
  • 1
  • 6
  • 7
47
votes
2 answers

Docstrings when nothing is returned

What is the docstring convention when a function doesn't return anything? For example: def f(x): """Prints the element given as input Args: x: any element Returns: """ print "your input is %s" % x return What should…
Ricky Robinson
  • 21,798
  • 42
  • 129
  • 185
47
votes
5 answers

How do I reference a documented Python function parameter using Sphinx markup?

I'd like to reference a previously-documented function parameter elsewhere in a Python docstring. Consider the following (admittedly completely artificial) example: def foo(bar): """Perform foo action :param bar: The bar parameter """ …
Inactivist
  • 9,997
  • 6
  • 29
  • 41
46
votes
4 answers

Python docstrings to GitHub README.md

How do I transcode Python documentation strings to a GitHub readme.md file? Even though it seems like something everyone does, I cannot seem to get a decent solution and I am assuming it should be easy, so it seems unlikely folks are going throw two…
Matteo Ferla
  • 2,128
  • 1
  • 16
  • 27
46
votes
4 answers

Docstrings vs Comments

I'm a bit confused over the difference between docstrings and comments in python. In my class my teacher introduced something known as a 'design recipe', a set of steps that will supposedly help us students plot and organize our coding better in…
Jeremy
  • 613
  • 1
  • 6
  • 7
38
votes
3 answers

Utilizing docstrings

It's a newbie question, but I didn't manage to google anything reasonably concise yet enlightening on the subject. I've got Sublime Text editor and an excellent plugin DocBlockr https://github.com/spadgos/sublime-jsdocs , which makes proper…
Alexey Orlov
  • 2,412
  • 3
  • 27
  • 46
1
2
3
49 50