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

Docstring has inconsistent leading whitespace

In the following code: def read_file(filename): """ >>> read_file('text.txt') {'Donald Trump': [('Donald Trump', 'Join me live in Springfield, Ohio!\nLit!!\n', 1477604720, 'Twitter for iPhone', 5251, 1895)]} """ I get an error…
Monty
  • 197
  • 2
  • 10
5
votes
1 answer

pylint prints warning about missing module docstring while it has

The first lines of multisql.py: #!/usr/bin/env python3 """Execute SQL on multiple servers """ # -*- encoding: utf-8 -*- import os ... pylint warning: [mutex@thinkpad multisql] @ $ pylint *.py No config file found, using default…
pensnarik
  • 1,214
  • 2
  • 12
  • 15
5
votes
1 answer

Extending or overwriting a docstring when composing classes

I have a class MyClass: class MyClass(object): def __init__(self): pass def my_function(self, x): # MyClass.my_function.__doc__ is not writable! # Otherwise, I could just set it here. Origin.func(self,…
Xiphias
  • 4,468
  • 4
  • 28
  • 51
5
votes
1 answer

Any reason to put code before the docstring in Python?

I see in some other people's Python code that they paste some checks before the docstring of the function; something like this: def func(args): if x_version != 1.7: return """docstring is here""" # function body code # ... Are there any…
Alex Tereshenkov
  • 3,340
  • 8
  • 36
  • 61
5
votes
2 answers

Can I format the docstring in python3

I am trying to make a docstring that will accept replacement fields as follows def run_tests(args): """run tests on methods in {0} usage: {0} --tests """.format(__file__) pass but when I run help(run_tests) in the interpreter, I do…
posop
  • 511
  • 2
  • 7
  • 17
5
votes
5 answers

More than 1 docstrings for a single module/function etc.?

I'm using python 3.1. Is it possible to create more than 1 docstring for a single module or function? I'm creating a program, and I'm intending to have multiple docstrings with a category for each. I intend to give other people the program so they…
Eric
  • 5,686
  • 2
  • 23
  • 36
5
votes
1 answer

Why can't I use the format function with docstrings?

I have a function that starts like this: def apply_weighting(self, weighting): """ Available functions: {} """.format(weightings) What I want is for the docstring to print the dictionary of available weighting functions. But when…
Jimmy C
  • 9,270
  • 11
  • 44
  • 64
5
votes
3 answers

generate python docstring template using current argument list

I have a python package to release soon, so now I begin to slowly add docstrings to the files using vim. So for a func like def foo(x, y, z=None, **kwargs): I have to manually type the following repetitively for a whole day """ foo does this…
nye17
  • 12,857
  • 11
  • 58
  • 68
5
votes
1 answer

Canonical way in Python to add or retrieve meta information to/from docstrings?

I'd like to add some meta information to my Python modules' docstrings, such as author, email, version. Is there a canonical way to do that? I searched quite a while, but couldn't find anything with definite authority, here or on the web. PEP 426 --…
Stefan Ludwig
  • 333
  • 2
  • 8
5
votes
2 answers

Can Sublime color Python docstrings differently from single-line strings?

I'd like to differentiate between Python docstrings and single-line strings in Sublime Text 2. Looking at the Python language definition, I can see this, along with a matching definition for apostrophe-strings that uses the same…
DNS
  • 37,249
  • 18
  • 95
  • 132
4
votes
2 answers

Extracting "extra" docstrings from Python code?

Python docstrings that immediately follows the declaration of a class or function are placed in the __doc__ attribute. The Question: How does one extract additional "internal" docstrings that occur later on in a function? Update: Such literal…
Graham
  • 876
  • 1
  • 9
  • 12
4
votes
1 answer

Parsing Python Module Docstrings

Is it possible to parse module-level docstrings with the AST? I am working on a python documenter here and visiting the module tokens and grabbing the documentation does not yield the module-level docstring. So far, I've had to resort to importing…
mvanveen
  • 9,754
  • 8
  • 33
  • 42
4
votes
1 answer

Automatically generate API reference for all subpackages / modules

I am using mkdocs with the mkdocstrings plugin to generate the documentation of my Python package. My package is organized in a standard fashion - setup.py - mkdocs.yaml - docs/ - mypackage/ - __init__.py - module1.py - module2.py -…
Vince
  • 3,979
  • 10
  • 41
  • 69
4
votes
1 answer

How to write docstring summary on multiple lines without triggering D205 and D400?

Say that I have def foo() """This is the first part of the summary, and this is the second part of the summary. This is the description. """ I got both D205 and D400 docstring style warnings triggered. I want to be able to break…
Barzi2001
  • 989
  • 8
  • 24
4
votes
0 answers

What does the symbol @ mean in Python type hinting?

As shown in image below, what do _S@map and _T@map mean? Additional information: Looking at multiprocessing.Pool.map definition there is not type hints, so the types above must be automatically generated by Pylance, which is the language server I…
Leonardus Chen
  • 1,103
  • 6
  • 20