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

Proper Google docstring for lists

I've been using the Google Docstring format described here but I'm wondering if there is an agreed upon method for documenting lists of a known type. I've been using def function(a_list) """ Args: a_list (list[dict]): a list of…
Eric Blum
  • 744
  • 12
  • 29
7
votes
1 answer

What is the use of PyCharm's docstring template? How do I use it effectively?

PyCharm, the Python IDE generate a template for docstring as I start to type in the docstring. This is the template generated for a simple function. def add_them(x, y): """ :param x: :param y: :return: """ z = x + y …
user3348051
7
votes
1 answer

How to document kwargs using epytext for the auto completion hinting in PyCharm?

Is is possible to get an additional hint for kwargs, which will give you examples of predefined possible keyword arguments? Maybe epytext is not supporting it? class Person(): def __init__(self, **kwargs): """ @param name: Name …
MagSec
  • 346
  • 4
  • 17
7
votes
4 answers

Replacing python docstrings

I have written a epytext to reST markup converter, and now I want to convert all the docstrings in my entire library from epytext to reST format. Is there a smart way to read the all the docstrings in a module and write back the replacements? ps:…
tomaz
  • 141
  • 2
  • 6
7
votes
2 answers

How to modify class docstrings using classmethods

My Problem: I've created a series of nodes that each have a set of attribute objects associated with them. The attribute objects are initialized with descriptions and names for each attribute. I'd like these attributes, and their descriptions, to…
6
votes
3 answers

Getting doc string of a python file

Is there a way of getting the doc string of a python file if I have only the name of the file ? For instance I have a python file named a.py. I know that it has a doc string ( being mandated before) but don't know of its internal structure i.e if…
johnny alpaca
  • 75
  • 1
  • 4
6
votes
2 answers

How to print docstring for class attribute/element?

I have a class: class Holiday(ActivitiesBaseClass): """Holiday is an activity that involves taking time off work""" Hotel = models.CharField(max_length=255) """ Name of hotel """ I can print the class docstring by…
Toms Code
  • 1,439
  • 3
  • 15
  • 34
6
votes
1 answer

More consistent Sphinx themes

Consider the following function # in mymodule.py: def myfunc(num,mystring): """ replicates a string :param num: a positive integer :param mystring: a string :return: string concatenated with itself num times :Example: …
l7ll7
  • 1,309
  • 1
  • 10
  • 20
6
votes
1 answer

Put docstrings on special methods?

I'm trying to decide what information to put in the class docstring and what to put in the __init__ method docstring. Up until now I've been putting an overview of the class and how to work with it in the class docstring, while stuff directly…
Lauritz V. Thaulow
  • 49,139
  • 12
  • 73
  • 92
6
votes
2 answers

Is there a docstring autocompletion tool for jupyter notebook?

I am looking for a tool/extension that helps you writing python docstrings in jupyter notebook. I normally use VS code where you have the autodocstring extension that automatically generates templates (e.g. the sphinx or numpy template) for…
Ottotos
  • 633
  • 5
  • 14
6
votes
1 answer

Python Google docstring format: more than one type for argument?

I am using Google-style python docstring format. There's one function, and the input parameter could be dict, list, or string. What is the format of multiple data type in docstring? Like Args: input (str, list, dict): input of this function
user19881219
  • 317
  • 2
  • 16
6
votes
2 answers

python comment in docstring

I found this one out because some of the homework questions I met were tested by docstrings, and it gives me failures. For example: def foo(x): """ >>> foo(5) 25 >>> foo(6) 36 # Are you sure? """ return x**2 if __name__…
jxie0755
  • 1,682
  • 1
  • 16
  • 35
6
votes
1 answer

Is there a way to keep docstrings separate from the functions they document?

I'm working on a module with many small functions but whose docstrings tend to be quite long. The docstrings make working on the module irritating as I have to constantly scroll over a long docstring to find a little bit of actual code. Is there a…
hwiechers
  • 14,583
  • 8
  • 53
  • 62
6
votes
1 answer

sphinx: Including .tex file via raw:: latex

I want to include latex doc in sphinx. sphinx html build does not include the latex file linked using .. raw:: latex directive. I have this is my dir structure docs/ source/ importlatex.rst index.rst build/ tex/ …
muon
  • 12,821
  • 11
  • 69
  • 88
6
votes
1 answer

Special Characters and Line Breaks in Python Doctest

I have a function with a docstring that looks like the following and I want to test that the docstring is correct. I am currently using the doctest module to do so. However, I cannot find a way to represent new line characters and line breaks in the…
Bob Marshall
  • 453
  • 1
  • 6
  • 25