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

How to view a Python class docstrings using jupyter notebook?

In the spyder IDE it is possible to press ctrl+i when instantiating a class to bring up the docstrings associated with that class. Is there a similar feature in jupyter notebook?
CiaranWelsh
  • 7,014
  • 10
  • 53
  • 106
25
votes
2 answers

How to find annotations in a PHP5 object?

I would like to be able to implement custom annotations in my PHP5 objects, and I'd like to learn how the whole process works by building my own parser. To start, though, I need to know how to FIND the annotations. Is there a Reflection method that…
johnnietheblack
  • 13,050
  • 28
  • 95
  • 133
24
votes
2 answers

Docstrings - one line vs multiple line

I'm adding some (epydoc) documentation to a package I've written, and I'm coming across a lot of instances where I'm repeating myself a multitude of times. def script_running(self, script): """Return if script is running @param script:…
Alex L
  • 8,748
  • 5
  • 49
  • 75
24
votes
2 answers

Python and sphinx: bullet point list in multiline google style docstring

I am currently documenting my Python project using Sphinx. I have come across an issue when including a bullet point list in the multi-line part of a docstring. I would like to include a bulleted list, but one of the items is quite long. I would…
Pierre Massé
  • 693
  • 1
  • 5
  • 23
24
votes
2 answers

Formatting python docstrings for dicts

What's the recommended way of adding a docstring for a dictionary parameter? I can see multiple line docstring examples here. I need to document the input arguments to a function in the docstring. If it's a simple variable, I can use something…
webminal.org
  • 44,948
  • 37
  • 94
  • 125
23
votes
1 answer

Python Black code formatter doesn't format docstring line length?

I am running the Black code formatter against a Python script however it doesn't reformat the line length for docstrings. For example, given the following code: def my_func(): """ This is a really long docstring. This is a really long…
Daniel O.
  • 583
  • 5
  • 15
23
votes
1 answer

Jupyter-like docstring view in Atom.io

This question has to do with getting Jupyter Notebook docstring functionality to work in Atom. I appreciate any and all advisement. In the Jupyter Notebook you are able to access the docstring via shift-tab and expand and scroll through the…
reka18
  • 7,440
  • 5
  • 16
  • 37
22
votes
5 answers

How do you fix "Missing module docstringpylint(missing-module-docstring)"

I'm using the pygame module on VS Code and I ran into the issue where the pygame has not init member. I followed the solutions to this link. I edited the user settings and added "python.linting.pylintArgs": [ …
22
votes
3 answers

Docstrings for data?

Is there a way to describe the module's data in a similar way that a docstring describes a module or a funcion? class MyClass(object): def my_function(): """This docstring works!""" return True my_list = [] """This…
Bartosz Radaczyński
  • 18,396
  • 14
  • 54
  • 61
22
votes
6 answers

In Django RestFramework, how to change the Api Root documentation?

In django RestFramework, is there any "official" way to generate the documentation for the "Api Root" ? After looking at the RestFramework's source code, I've found a work around by subclassing the DefaultRouter: from rest_framework import…
Pierre-Jean Coudert
  • 9,109
  • 10
  • 50
  • 59
21
votes
2 answers

Tool for automatically check docstring style according to PEP257

Tools like pep8 can check source code style, but they don't check if docstrings are fromatted according to pep257, pep287. Are there such tools? Update I decided to implement such a static analysis tool on my own,…
Vladimir Keleshev
  • 13,753
  • 17
  • 64
  • 93
21
votes
1 answer

How to change the default module docstring in Spyder?

How to change the default template for new modules in Spyder IDE? # -*- coding: utf-8 -*- """ Created on %(date)s @author: X """
Abdulrahman Bres
  • 2,603
  • 1
  • 20
  • 39
21
votes
1 answer

Pycharm docstring: code references and docstring inheritance

I'm currently going through my project in Jetbrains Pycharm 2017.1.5, documenting all my python 3.6 classes and methods, and several things stand out to me about the docstring format. I want to link to other methods / functions / classes from some…
Cynadyde
  • 373
  • 3
  • 9
21
votes
2 answers

Automatically Generate GitHub Wiki Documentation from Python Docstrings

The title says it all. What I imagine is to have docstrings for all of my modules, classes and functions and somehow nicely navigate the doc via github (wiki?). Also, the doc should be in sync with latest code meaning it should be…
Giampaolo Rodolà
  • 12,488
  • 6
  • 68
  • 60
21
votes
2 answers

"Expected an indented block" error?

I can't understand why python gives an "Expected indentation block" error? """ This module prints all the items within a list""" def print_lol(the_list): """ The following for loop iterates over every item in the list and checks whether the list…
kartikeykant18
  • 1,737
  • 6
  • 28
  • 42