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

Docstring for callable class in Python

I am trying to document a Python callable class by using docstring so that I can auto-generate documentation in Sphinx. I am using the autoDocstring plugin for generating docstring stubs. For classes featuring __call__ method it seems to format the…
MaciekS
  • 401
  • 8
  • 18
4
votes
1 answer

vscode docstrings not appearing properly

I have no idea why I am experiencing this issue. In past projects (On a different computer with a different OS) I have had no issues displaying docstrings. When I write a docstring, it should show something like this. Instead, my docstrings are…
Mike
  • 1,471
  • 12
  • 17
4
votes
1 answer

Is there a way to describe/type-hint the contents of a function's parameters?

I'm trying to learn how to better document my code. Describing a function and just hinting that it receives dict seems to leave any future reader rather short on information though. Is it common at all to do the following? Or is there maybe another…
4
votes
1 answer

How can I document type parameters in Python?

I'd like to document what T means in this class from typing import Generic, TypeVar T = TypeVar('T') class A(Generic[T]): pass I could document T with T.__doc__ = "Represents ..." but I might want to use T it in several unrelated classes. Is…
joel
  • 6,359
  • 2
  • 30
  • 55
4
votes
1 answer

Specify expected outcome in a docstring as hexadecimal?

Is there a way to specify expected integer outcomes in a docstring in hex notation? def identity(val): """ >>> identity(243) 243 >>> identity(243) 0xf3 """ return val if __name__ == "__main__": import doctest …
Bernhard Wagner
  • 1,681
  • 12
  • 15
4
votes
1 answer

Use pandas.read_csv docstring in the function I'm writing

I'd like to write a function with the following header : def split_csv(file, sep=";", output_path=".", nrows=None, chunksize=None, low_memory=True, usecols=None): As you can see, I am using the same parameters as several found in pd.read_csv. What…
Imad
  • 2,358
  • 5
  • 26
  • 55
4
votes
2 answers

How to selectively override the text from python's help(MYclass) with something shorter and customized?

I'm learning to write user-friendly classes and methods and I'd like for users to know how to use them from a terminal. Python's standard help(MYclass) returns eighteen lines I don't want, and that's half a screen for a small window and those just…
uhoh
  • 3,713
  • 6
  • 42
  • 95
4
votes
1 answer

how to change the test description of python (2.7) untitest

It seems that the unittest module has been changed a lot in Python 2.7 I have a test case: class DemoTest(unittest.TestCase): def test_foo(self): """Test foo""" pass The console output is: Test foo ... ok After upgrading to Python…
stanleyxu2005
  • 8,081
  • 14
  • 59
  • 94
4
votes
1 answer

Documenting exceptions that can happen in other functions in python docstrings

In Python, should we document in the docstrings exceptions that can be raised in other functions/classes besides the ones that are raised in the body of current function/method? Obs.: I'm considering the Google Python docstring style…
Raphael Philipe
  • 171
  • 2
  • 5
4
votes
1 answer

Reducing redundancy in Python doc-strings while using pylint docparams

I'm writing some Python code that's being checked with pylint with the docparams extension. This requires documenting function parameters in their docstrings. I have a tendency to try to decompose functions as much as possible and end up with lots…
igal
  • 646
  • 7
  • 17
4
votes
1 answer

Use a variable to define multiple functions docstring

I have several functions (a, b and c), I want them to use the same docstring. So my plan was to conserve lines by only writing the docstring once and save it to a variable DOCSTRING. Then I place it under the function declaration. I hadn't found…
Taku
  • 31,927
  • 11
  • 74
  • 85
4
votes
1 answer

Obtain Python docstring of arbitrary script file

I am writing a module that requires the docstring of the calling script. Thus far I have managed to obtain the filename of the calling script using import inspect filename = inspect.stack()[1].filename The docstring can be found inside the calling…
Octaviour
  • 745
  • 6
  • 18
4
votes
2 answers

about python __doc__ docstring

i want to show docstring of my function, but if i use like this @cost_time def func(): "define ...." blabla print func.__doc__ it will not show the docstring,just because i use some meta programming tricky, how can fix this?
mlzboy
  • 14,343
  • 23
  • 76
  • 97
4
votes
3 answers

How can three double quotation marks be stored in a Python docstring?

I have a large ASCII logo that I want to store in a docstring. This logo features multiple instances of three consecutive double quotation marks. The size and complexity is such that escaping individual characters isn't a realistic option. Given…
d3pd
  • 7,935
  • 24
  • 76
  • 127
4
votes
2 answers

Python docstrings templated

Why doesn't dynamically formatting docstrings work? Is there an acceptable workaround for doing this at function definition time? >>> DEFAULT_BAR = "moe's tavern" >>> def foo(bar=DEFAULT_BAR): ... """ ... hello this is the docstring ... …
wim
  • 338,267
  • 99
  • 616
  • 750