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
8
votes
2 answers

Is docstring max line-length different to normal PEP8 standard?

So im looking at some code and bringing it up to PEP 8 standard with the help of pylint and i noticed that if i was using triple quotes for a print statement where the text went past 120 chars (we are allowing 120 instead of 79) pylint didn't…
Jacxel
  • 1,634
  • 8
  • 22
  • 33
7
votes
1 answer

Doctest for nested docstring

Suppose I have following code: def foo(s): """A dummy function foo. For example: >>> a = '''This is a test string line 1 This is a test string line 2 This is a test string line 3''' >>> foo(a) This is a test string line 1 This is a test string…
user1045217
  • 506
  • 1
  • 4
  • 9
7
votes
0 answers

Is there a way to auto-update python auto-docstrings with new/modified parameters?

I am using VS Code as my editor and autoDocstring as the auto-docstring generator extension for Python. There are times when I modify the parameters of a function, either by introducing new parameters or by modifying their names, default values etc.…
Anirban Chakraborty
  • 539
  • 1
  • 5
  • 15
7
votes
1 answer

Is there a way to make Eclipse+PyDev display function documentation like Python's help() would?

I would like to force Eclipse to show Python's help output in the popup instead of the whole function, i.e. given: def myFunc(arg): '''Function description''' return 1 I would like the code assist popup to contain only "Function…
zmk
  • 504
  • 6
  • 6
7
votes
2 answers

How can I document methods inherited from a metaclass?

Consider the following metaclass/class definitions: class Meta(type): """A python metaclass.""" def greet_user(cls): """Print a friendly greeting identifying the class's name.""" print(f"Hello, I'm the class…
Alex Waygood
  • 6,304
  • 3
  • 24
  • 46
7
votes
1 answer

PyCharm not generating docstrings and no settings under Python Integrated Tools

I'm trying to generate the docstrings to document my python functions, but when I'm going to PyCharm > Settings > Tools > Python Integrated Tools I find an empty window with no options and in particular no Docstrings > Docstring format >…
Gerardo Zinno
  • 1,518
  • 1
  • 13
  • 35
7
votes
2 answers

Python Sphinx: How to embed code into a docstring?

How can I embed code into a docstring to tell Sphinx to format the code similar as it will be done in Markdown (different background colour, monospaced sans font)? For example to document a code usage example. """ This is a module documentation Use…
Alex44
  • 3,597
  • 7
  • 39
  • 56
7
votes
1 answer

How to auto generate docstrings in PyCharm for dataclasses?

Is there a way for auto generating docstrings for dataclasses in the same fashion of the method and function docstrings? I did not find anything useful through help / search from dataclasses import dataclass @dataclass class ExtractionConfig: …
user12690225
7
votes
2 answers

Should I list class methods in the class docstring?

I am a bit confused by the PEP257 standard for documenting classes. It says, "The docstring for a class should summarize its behavior and list the public methods and instance variables" But it also says that all functions should have dosctrings…
Phil Evans
  • 790
  • 9
  • 18
7
votes
1 answer

Using type aliases in docstrings

Is there a best practice for using type aliases or typing objects in docstrings? This question might attract opinion based answers. But it could also be that there is a widely accepted convention or external tool support for a particular…
actual_panda
  • 1,178
  • 9
  • 27
7
votes
1 answer

How to format long lines of argument description in Google docstring style

This is an excerpt from a Google style docstring: def function_with_types_in_docstring(param1, param2): """Example function with types documented in the docstring. `PEP 484`_ type annotations are supported. If attribute, parameter, and …
Ivo
  • 71
  • 1
  • 3
7
votes
3 answers

Is it a good practice to use triple quotes to create "docstrings" in non-standard contexts?

I am looking at someone's code which has this kind of "docstrings" all over the place: SLEEP_TIME_ON_FAILURE = 5 """Time to keep the connection open in case of failure.""" SOCKET_TIMEOUT = 15 """Socket timeout for inherited socket.""" ... As per…
codeforester
  • 39,467
  • 16
  • 112
  • 140
7
votes
2 answers

Is there a way to print a short version of the docstring for all members of a Python object?

In Python dir() returns the list of names in the current local scope. __doc__ returns the complete docstring of an object. How can I list all names in the current local scope and print the first line of each item's docstring ? To elaborate: for…
Josh Reuben
  • 289
  • 2
  • 8
7
votes
1 answer

PyCharm not displaying Google style docstrings in tooltips

I would like to use Google style docstrings for Python. I've specified that Pycharm should expect Google docstrings: For some reason PyCharm will not show my docstrings in tooltips: I'm referencing the examples here for the proper Google docstring…
Automaton
  • 168
  • 9
7
votes
1 answer

Python-Sphinx: "inherit" method documentation from superclass

Edit: As of now (Sphinx 1.4.9) there seems to be no way to tell Sphinx to do what I want (see issue on GitHub). The accepted answer from Brecht Machiels solves the problem in an other way, until Sphinx might be able to do so one day. Description: I…
AlexV
  • 578
  • 8
  • 19