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

How to auto generate docstring snippets for python modules in VsCode?

I have installed the Visual Studio Code extension Python Docstring Generator that autogenerates docstring snippet. Customized settings that I use: "autoDocstring.docstringFormat": "sphinx", "autoDocstring.generateDocstringOnEnter":…
Amin Ba
  • 1,603
  • 1
  • 13
  • 38
4
votes
2 answers

Sphinx documentation showing double-colon in fields

I am developing documentation for a package and when I build the sphinx docs I'm getting double colons for all of the fields for every function definition. I am using the numpydoc style for my docstrings and there are no colons in those docs to…
Nick Roach
  • 41
  • 3
4
votes
3 answers

Accessing __doc__ of function inside a lambda

I would like to extract the docstring of a function once it has been wrapped in lambda. Consider the following example: def foo(a=1): """Foo docstring""" return a dct = { "a": foo, "b": lambda: foo(2), } for k, v in dct.items() …
yellowhat
  • 429
  • 6
  • 17
4
votes
3 answers

what is the _correct_ way to write "a or b" kind of type hints in docstring?

I have an argument which is either "E" or "H". def my_func(name): """ Parameters ---------- name : str """ To specify it's either 'E' or 'H' I've seen people writing: name : {'E', 'H'} name : ['E', 'H'] name : ['E'…
Alex
  • 912
  • 2
  • 7
  • 19
4
votes
1 answer

duplicate information in typing and docstring?

I am confused about the use of type hints and docstrings. Aren't they duplicate information? For example: def my_func(name: str): """ print a name. Parameters ---------- name : str a given name """ …
Alex
  • 912
  • 2
  • 7
  • 19
4
votes
0 answers

How to document acceptable dictionary keys in a parameter passed to a function/method?

I have a function that accepts a dictionary as a parameter. This dictionary may contain keys that are used to configure how the function processes data. In docstring, I would like to specify which keys are acceptable. I am using PyCharm and its…
Vaclav Pelc
  • 582
  • 6
  • 20
4
votes
1 answer

Doctest for dynamically created objects

What is the best way to test code like this (the one below obviously fails while object is created in different block every time): def get_session(db_name, verbose, test): """Returns current DB session from SQLAlchemy pool. >>>…
Piotr Byzia
  • 3,363
  • 7
  • 42
  • 62
4
votes
0 answers

PyCharm Docstring :func: and :meth: not working

I am writing a documentation in my project's code but the inline link to a function/method doesn't work. It simply doesn't show up as expected when I use CTRL + Q: EDIT: Code: class Editor: def picture(self): """ The same as…
Stephirio
  • 51
  • 4
4
votes
1 answer

LazyDocs - Generated *.md files do not represent bullet list

I hope I am right here in this channel/tag. I am using lazydocs for automatic generation of my md files. My project is written in Python 3.7.5. I do have some bullet lists in my docstrings. According to this example I need to leave a blank line,…
o0minni0o
  • 43
  • 2
4
votes
2 answers

How to handle rounding to negative zero in Python docstring tests

Related to this question: How to have negative zero always formatted as positive zero in a python string? I have the following function that implements Matlab's orth.m using Numpy. I have a docstring test that relies on np.array2string using…
DStauffman
  • 3,960
  • 2
  • 21
  • 30
4
votes
1 answer

Can I use the argparse help string to decribe my script's return values?

I know I can use argparse's help string to describe both the general purpose of my script, and its input arguments. Is there also a built-in / conventional method with which I can describe my script's return values?
aon
  • 362
  • 2
  • 10
4
votes
1 answer

Embed a plantuml diagram somewhere in a docstring (with the Sphinx plantuml extension)

I installed the sphinxcontrib-plantuml extension which works fine if I run the HTML builder e.g. with following testmodule.rst file for a corresponding testmodule.py file: testmodule module ================= .. automodule:: testmodule …
El tornillo
  • 437
  • 3
  • 16
4
votes
1 answer

How to auto generate function docstring in jupyter

I am using Jupyter and I was wondering if there is a way to auto generate doc string for a function? Seems like PyCharm and Spyder has that functionality but I can't find one for Jupyter notebook. I want to have something like this def…
Young
  • 97
  • 5
4
votes
0 answers

Python IDE - docstring with PyCharm and vs-code

I recently switched to use PyCharm as my main python IDE. I am having a problem on generating docstrings. I usually follow the Google standard, which is supported natively on PyCharm and with an extension on vscode…
Guido Muscioni
  • 1,203
  • 3
  • 15
  • 37
4
votes
1 answer

Python Google-Style DocString for Function with No Arguments

I've been using the Google-Style Python Docstring format for a while now. The way I've been handling functions/methods with no arguments suddenly doesn't look correct to me. I did a bit of searching and couldn't find anything online that specified…
Eric Le Fort
  • 2,571
  • 18
  • 24