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
15
votes
4 answers

How to docstring in python for multiple languages

I want to create a new module for myself but I also want some colleagues to be able to use it. I started writing my docstrings in English but then I realized that it will make the module useless for some of them who are not good understanding this…
14
votes
2 answers

How to make PyCharm get type hints from function definition and populate type values in docstrings?

I always use type hints in function definitions, for example: def foo(a: int, b: str) -> bool: pass When I use PyCharm auto docstring generator to make docstrings in my code, I get this: def foo(a: int, b: str) -> bool: """ :param a: …
Hanif
  • 140
  • 1
  • 6
14
votes
2 answers

PyCharm: How to get class attributes to be listed in Quick Documentation pop-up?

I found this really annoying that Quick Documentation doesn't show attributes, listed in class' docsting. I thought that the reason was incorrect or unsupported docstring format, but behavior is the same for both reST and Google styles (I set them…
Egor Panfilov
  • 159
  • 1
  • 8
14
votes
1 answer

Why doesn't Python auto escape '\' in __doc__?

It seems that some escape chars still matter in docstring. For example, if we run python foo.py (Python 2.7.10), it will emit error like ValueError: invalid \x escape. def f(): """ do not deal with '\x0' """ pass And in effect, it…
Hongxu Chen
  • 5,240
  • 2
  • 45
  • 85
14
votes
1 answer

How to document fortran function for f2py?

I would like to use docstring or something similar to document my fortran routines which can be usable with the python help command. The autogenerated docstring made by f2py is very not sufficient and I need to add more details in the same way we do…
Sigmun
  • 1,002
  • 2
  • 12
  • 23
14
votes
3 answers

Parsing function docstring in sphinx.autodoc format

I have many functions in Python of the type: def foobar(one, two): """ My function. :param int one: My one argument. :param int two: My two argument. :rtype: Something nice. """ return 100 + one + two And I need to parse…
Havok
  • 5,776
  • 1
  • 35
  • 44
14
votes
4 answers

Inherit a parent class docstring as __doc__ attribute

There is a question about Inherit docstrings in Python class inheritance, but the answers there deal with method docstrings. My question is how to inherit a docstring of a parent class as the __doc__ attribute. The usecase is that Django rest…
Reinout van Rees
  • 13,486
  • 2
  • 36
  • 68
14
votes
1 answer

Referencing parameters in a Python docstring

I use Sphinx and the autodocs feature to ensure we have good docs in our project. So I'm familiar with info field lists and I'm familiar with using cross-referencing in our docs. However, when writing docstring for a method or function I find it…
Dwayne
  • 845
  • 7
  • 11
13
votes
1 answer

View reStructuredText (Sphinx) docstrings in iPython?

Is there a way to have IPython fully render reStructuredText (for Sphinx) docstrings when you type the following: help foo or: foo? I find it super distracting to try to read help docs in IPython when it shows the raw markup. reST is great for…
jkmacc
  • 6,125
  • 3
  • 30
  • 27
13
votes
1 answer

How do you specify a Pandas DataFrame schema/structure in a docstring?

I'd like to describe the DataFrame structure my Python function expects, and a verbal description like: def myfun(input): """ Does a thing. Parameters ---------- input : pandas.DataFrame column 1 is called 'thing1' and it is…
jkmacc
  • 6,125
  • 3
  • 30
  • 27
13
votes
1 answer

Pytesting Doctests in Visual Studio Code Debugger

Suppose I have the following code in foo.py: def start(): """ >>> start() Hello world """ test = 10 print('Hello world') Normally, I would run the doctest by running pytest foo.py --doctest-modules -v in the terminal. I…
Oliver Leung
  • 185
  • 1
  • 6
13
votes
2 answers

In Python Docstrings, What Does `:obj:` do?

I keep seeing docstrings that have lines that read like this: param : :obj: str I can't find a reference to what :obj: stands for or does. It seems like it would denote a str object, but I also see param : int which doesn't seem to jibe. Thanks.
Paka
  • 185
  • 2
  • 8
13
votes
1 answer

Python docstring for descriptors

I am using descriptors to define the registers of an interface class: class Register(object): def __init__(self, address, docstring="instance docstring"): self.address = address self.__doc__ = docstring def __get__(self,…
Leonhard Neuhaus
  • 228
  • 3
  • 11
13
votes
2 answers

Should the docstring only contain the exceptions that are explicitly raised by a function?

When writing doc strings in python, I am wondering if the docstring should contain the exceptions that are implicitly raised or if it should also contain the exceptions I explicitly raise. Consider the function def inv(a): if a == 0: …
Quickbeam2k1
  • 5,287
  • 2
  • 26
  • 42
13
votes
1 answer

How to format Django views' docstring

I use simple views in my project, and now my project begin to be bigger, I'm writing a sphinx doc and use objects docstrings with sphinx.ext.autodoc for inclusion. My trouble is: How to greatly format view's docstring ? Document GET/POST and more…
Zulu
  • 8,765
  • 9
  • 49
  • 56