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

What is a correct way to override child __init__ function?

Given a child-parent structure like so: class Parent: def __init__(self, param1=1, param2=2, param3=3, param4=4): """ Parent docstring here :param param1: param1 stuff :param param2: param2 stuff :param…
Raven
  • 648
  • 1
  • 7
  • 18
0
votes
1 answer

How do I convert Python docstrings to a different style?

I am working on a codebase which uses Google style Python docstrings, and I would like to convert them automagically to numpy style. Is there an automatic tool for doing so? A quick Google search reveals pyment, but I'm not sure if that's…
redhotsnow
  • 85
  • 8
0
votes
0 answers

How to programmatically define python wrapper functions that have the same (optional) parameters of the functions they wrap

I find myself defining a number of wrapper functions, mainly to avoid modifying functions that come from packages (either standard as numpy or my own). I would like the wrapper to be understood by the interpreter as to have the same arguments as the…
Rho Phi
  • 1,182
  • 1
  • 12
  • 21
0
votes
1 answer

What are the possibilities to add souce documentation on a module/folder level?

In iOS/Xcode projects it's common to use docstrings with Markdown for documenting methods, properties, protocols etc. within one source file. However, I couldn't find any proper way for documenting a "folder" of files. E.g. I have sorted my app's…
Beat Rupp
  • 319
  • 3
  • 12
0
votes
1 answer

How to document python module using reStructuredText?

I want to know how is the correct way to document a python module using reStructuredText format. In PEP there is a general description but no explanation about format for module documentation.
eduardosufan
  • 1,441
  • 2
  • 23
  • 51
0
votes
1 answer

Why should I use comments when documenting python?

There are two ways to document a module, function, or class in the code itself: comments and docstrings. Docstrings are more functional, as they can be accessed via the help() function, while comments can only be accessed in the source code. When…
Alec
  • 8,529
  • 8
  • 37
  • 63
0
votes
1 answer

Function summary in Google format not showing up in the quick documentation window in PyCharm

PyCharm 2018.3 has an option to change your docstring format under File > Settings > Tools > Python Integrated Tools > Docstring Format After selecting 'Google', I write code like: def my_func(a, b): """ Do something with a and b Args: …
Raghav Mehta
  • 203
  • 2
  • 14
0
votes
0 answers

How to change an inherited method's docstring in Python?

Suppose I have a class structure like the following: class BaseClass(object): def divide_reward_by_x(self, x, data): """ Args: x: float Return: Reward value """ return self.reward(data) // x class…
NewNameStat
  • 2,474
  • 1
  • 19
  • 26
0
votes
2 answers

How do you use write() in python to write a docstring to a file?

I am using pytest to check for multi-line docstrings and the way I am testing to check for these multi-line comments involves making a temp file and using write() to write the docstring in and then search for it. def…
0
votes
1 answer

Trouble printing a docstring after importing module on command line

I am having trouble printing a docstring after importing it into my terminal window using the python interpreter. After typing python in my terminal window I enter in the following code: import pizza print(make_pizza.__doc__) The print statement…
0
votes
2 answers

What is the type of a parameter that is an abstract class name in Python docstrings?

EDITED: Let's say I have some classes that inherit from the SuperFoo abstract class: from abc import ABCMeta, abstractmethod class SuperFoo(object): __metaclass__ = ABCMeta @abstractmethod def do_something(): pass class…
Gerardo Figueroa
  • 549
  • 2
  • 6
  • 16
0
votes
0 answers

Sphinx exclude members with include directive using

This is my project structure: base/devices.py users/devices.py company/devices.py Here is my index.rst: .. My documentation master file. Welcome to documentation! ========================= .. toctree:: :maxdepth: 3 :caption:…
ncopiy
  • 1,515
  • 14
  • 30
0
votes
1 answer

Helpstring in argparse, each string from new line

I am doing a CLI utility. When adding a docstring to call help for the module with the function -- help in console, I was faced with the fact that all the added text is displayed as a continuous, unbreakable message. How to separate strings from…
Jekson
  • 2,892
  • 8
  • 44
  • 79
0
votes
1 answer

regex don't match docstring if commented

I want to match this (including whitespace) from: def foo(): """ this """ pass but in this case i don't want to match anything: # def foo(): # """ # this # """ # # pass At the moment I am using """(.*?)""" with the…
Cemu
  • 51
  • 2
0
votes
0 answers

Accessing class attributes docstring for argparse

I am currently using argparse in a class much like this: class MyClass: P_OPT = "my-opt" """This is an option to my script.""" P_OPT_HELP = "This is an option to my script" """Description for argparse""" def __init__(self, **kwargs): …
Valentin B.
  • 602
  • 6
  • 18