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
1135
votes
6 answers

What are the most common Python docstring formats?

I have seen a few different styles of writing docstrings in Python, what are the most popular styles?
Noah McIlraith
  • 14,122
  • 7
  • 31
  • 35
553
votes
19 answers

How to comment out a block of code in Python

Is there a mechanism to comment out large blocks of Python code? Right now, the only ways I can see of commenting out code are to either start every line with a #, or to enclose the code in triple quotes: """. The problem with these is that…
gbarry
  • 10,352
  • 6
  • 33
  • 43
184
votes
14 answers

How do I disable "missing docstring" warnings at a file-level in Pylint?

Pylint throws errors that some of the files are missing docstrings. I try and add docstrings to each class, method and function, but it seems that Pylint also checks that files should have a docstring at the beginning of them. Can I disable this…
Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
174
votes
4 answers

Using javadoc for Python documentation

I am currently beginning with Python and I have a strong PHP background and in PHP I have took the habit of using javadoc as a documentation template. I was wondering if javadoc has its place as docstring documentation in Python. What are the…
JF Dion
  • 4,014
  • 2
  • 24
  • 34
171
votes
4 answers

How to document class attributes in Python?

I'm writing a lightweight class whose attributes are intended to be publicly accessible, and only sometimes overridden in specific instantiations. There's no provision in the Python language for creating docstrings for class attributes, or any sort…
intuited
  • 23,174
  • 7
  • 66
  • 88
120
votes
7 answers

Inherit docstrings in Python class inheritance

I'm trying to do some class inheritance in Python. I'd like each class and inherited class to have good docstrings. So I think for the inherited class, I'd like it to: inherit the base class docstring maybe append relevant extra documentation to…
Craig McQueen
  • 41,871
  • 30
  • 130
  • 181
103
votes
5 answers

How to document Python code using Doxygen

I like Doxygen to create documentation of C or PHP code. I have an upcoming Python project and I think I remember that Python doesn't have /* .. */ comments, and also has its own self-documentation facility which seems to be the pythonic way to…
Hanno Fietz
  • 30,799
  • 47
  • 148
  • 234
97
votes
5 answers

How can I print a Python file's docstring when executing it?

I have a Python script with a docstring. When the parsing of the command-line arguments does not succeed, I want to print the docstring for the user's information. Is there any way to do this? Minimal example #!/usr/bin/env python """ Usage:…
thias
  • 2,290
  • 3
  • 20
  • 20
93
votes
10 answers

Adding docstrings to namedtuples?

Is it possible to add a documentation string to a namedtuple in an easy manner? I tried from collections import namedtuple Point = namedtuple("Point", ["x", "y"]) """ A point in 2D space """ # Yet another test """ A(nother) point in 2D…
Rickard
  • 1,754
  • 1
  • 12
  • 17
88
votes
7 answers

Is there a consensus on what should be documented in the class and __init__ docstrings?

I did not find any best practice about what should be documented in the class and __init__ docstrings. Sometimes I find that the constructor arguments are already documented in the class docstring, sometimes the are described in the __init__…
causa prima
  • 1,502
  • 1
  • 14
  • 24
86
votes
3 answers

Custom PyCharm docstring stubs (i.e. for google docstring or numpydoc formats)

Does PyCharm 2.7 (or will PyCharm 3) have support for custom docstring and doctest stubs? If so, how does one go about writing this specific type of custom extension? My current project has standardized on using the Google Python Style Guide…
kalefranz
  • 4,612
  • 2
  • 27
  • 42
85
votes
8 answers

How to print Docstring of python function from inside the function itself?

I want to print the docstring of a python function from inside the function itself. for eg. def my_function(self): """Doc string for my function.""" # print the Docstring here. At the moment I am doing this directly after my_function has been…
shane87
  • 3,090
  • 12
  • 51
  • 65
84
votes
4 answers

Python Docstring: raise vs. raises

I use the PyCharm IDE which assists with crafting PEP0257-compliant docstrings. It provides two attributes I don't entirely understand the distinction/use between: :raise Exception: exception explanation here :raises Exception: exception…
Bob Dylan
  • 1,773
  • 2
  • 16
  • 27
80
votes
5 answers

How to put a variable into Python docstring

So I'm trying to create a "dynamic" docstring which is something like this: ANIMAL_TYPES = ["mammals", "reptiles", "other"] def func(animalType): """ This is a sample function. @param animalType: "It takes one of these animal types %s" %…
Jack Z
  • 2,572
  • 4
  • 20
  • 17
75
votes
5 answers

Multi-line description of a parameter description in python docstring

So, reStructuredText is the recommended way for Python code documentation, if you try hard enough, you can find in the sphinx documentation how to normalize your function signature documentation. All given examples are single-line, but what if a…
1
2 3
49 50