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

How to stop PyCharm from populating docstrings?

If I add a docstring to a method using the triple-quote, as soon as I type a space after the triple-quote, PyCharm will populate the docstring with the parameters the method takes, and a return value, like so: def fill_blank(self, direction): …
shanusmagnus
  • 2,340
  • 2
  • 20
  • 19
13
votes
1 answer

How to spell check python docstring with emacs?

I'd like to run a spell checker on the docstrings of my Python code, if possible from within emacs. I've found the ispell-check-comments setting which can be used to spell check only comments in code, but I was not able to target only the…
gurney alex
  • 13,247
  • 4
  • 43
  • 57
13
votes
3 answers

python rtype docstring/restructured text for class factories/selectors

:rtype: specifies that this is the type of the returned object. Therefore, when I create the object obj in the following snippet I receive a warning from the IDE that cls is not callable, since the IDE expects that cls is object of type…
Tigra
  • 2,611
  • 20
  • 22
12
votes
1 answer

PyCharm not inserting docstring stub for class?

I have the following piece of code: class Note: def __init__(self, note=None, duration=None, start_time=None): self.note = note self.duration = duration self.start_time = start_time I wanted to generate a docstring for…
pyjamas
  • 4,608
  • 5
  • 38
  • 70
12
votes
1 answer

How can I retrieve the docstring for a property of a Python class instance?

Suppose I have a class like this: class TestCase(object): """Class docstring""" def meth(self): """Method docstring""" return 1 @property def prop(self): """Property docstring""" return 2 It's easy…
ali_m
  • 71,714
  • 23
  • 223
  • 298
12
votes
1 answer

Math expression in docstring is not rendered correctly by Sphinx

I've been trying to figure out what's wrong with this expression in my docstring. I'm using the sphinx.ext.mathjax extension in python sphinx v1.2b. The docstring: .. math:: w_k^* = \min_{w_k} \ell_k(w_k) + \lambda\left(\alpha||w_k||_1 +…
bluecat
  • 401
  • 5
  • 11
12
votes
6 answers

How to document structures in python?

If an argument to a function is expected to be a certain (or equivalent) structure, built using python's list, tuple and dict, how and where should it be documented? An example documentation: def foo(bar): """ Args: bar: 2-tuple,…
n611x007
  • 8,952
  • 8
  • 59
  • 102
12
votes
1 answer

How to specify docstring for __init__ in Python C extension

Perhaps a stupid question: how can one specify docstring for special functions like __init__ when writing a C extension? For ordinary methods, method table has provision for docstrings. The following autogenerated documentation is displayed when I…
subhacom
  • 868
  • 10
  • 24
11
votes
2 answers

Pylint message about module length reasoning and ratio of docstrings to lines of code

I know that this could be dismissed as opinion-based, but googling isn't finding the resources I was hoping for, and I am looking for links to any established and agreed best practices in the Python community. I am an intermediate Python programmer…
moink
  • 798
  • 8
  • 20
11
votes
2 answers

Sphinx: Link to a method of a class in another module in Python docstring

I want to add a link to a method of a class in one module (say module_2.py) in another method in another module ( say module_1.py). I want the link to work in Sphinx. Suppose: module_1.py class ABC: def foo(self): """ See docstring of…
user8215542
11
votes
3 answers

Unresolved reference in Django's docstring in PyCharm

I use Google Style Python Docstrings like in this Example for my Django's project. When i create a class and using an attributes notation in docstring, Pycharm always say - "Unresolved reference". class Post(models.Model): """ Class for…
Denis Savenko
  • 829
  • 1
  • 13
  • 29
11
votes
4 answers

Should you always document functions, even if redundant (specifically python)?

I try to use function names that are active and descriptive, which I then document with active and descriptive text (!). This generates redundant-looking code. Simplified (but not so unrealistic) example in python, following numpy docstring…
barford
  • 119
  • 1
  • 4
11
votes
2 answers

Clojure: How to get a function docstring?

I am trying to get ONLY the docstring of a function in Clojure, nevertheless I have encountered several problems as all the functions that I find actually print the function signature + docstring. So for example (doc map) will actually print…
carocad
  • 455
  • 6
  • 12
11
votes
3 answers

Python docstrings and inline code; meaning of the ">>>" syntax

I have some experience in Python but only recently came across extensive usage of docstrings. I'm going through the Financial Market Simulator (FMS) source code, and when I open it in PyCharm I see the following syntax highlighting (screenshot of a…
avg
  • 793
  • 2
  • 12
  • 24
11
votes
2 answers

Read The Docs not working with automodule

I've written some documentation for my project (in the Python docstrings), and tested everything with Sphinx on my local computer – everything works fine, all the import work correctly and so on. So I set up a custom environment on Read The Docs…
George Oblapenko
  • 878
  • 1
  • 10
  • 23