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

What style does PyCharm / IntelliJ use for Python docstrings?

I'm trying to understand the "solution ecosystem" with respect to creating docstrings, and then generating some nice reference documentation with PyCharm/IntelliJ. I'm used to the Google style docstrings, and am confused by PyCharm's default…
conner.xyz
  • 6,273
  • 8
  • 39
  • 65
3
votes
2 answers

automatically reformat docstrings to respect right margin

Often I edit a docstring and find my edit pushes a line's width past the desired right margin. As a result, many lines of text below this edit maybe need be reformatted before my docstring is once again acceptable. What's a simple and safe way to…
mathandy
  • 1,892
  • 25
  • 32
3
votes
1 answer

Is it possible embed pictures into docstring in Python?

Is it possible embed pictures into docstring in Python? I saw docpicture keyword somewhere, but can't find doc on it. Is it stabdartized or not? Where does picture should located? UPDATE I tried Sphinx notation, but my PyCharm shows i.e. it sees…
Dims
  • 47,675
  • 117
  • 331
  • 600
3
votes
2 answers

Structured python docstrings, IDE-friendly

In PHP I was used to PHPdoc syntax: /** Do something useful @param first Primary data @return int @throws BadException */ function($first){ ... — kinda short useful reference: very handy when all you need is just to recall 'what's that??',…
kolypto
  • 31,774
  • 17
  • 105
  • 99
3
votes
4 answers

Writing docstrings - specifying functions arguments and returns

Suppose I have a function, say: >>> def foo(a): return a+1 I want to write a documentation string for it. what is the convention in specifying in the docstring that it takes a and returns a+1?
user225312
  • 126,773
  • 69
  • 172
  • 181
3
votes
1 answer

Using string formatting within class method docstrings

I have a class with several similar methods, each with long docstrings that are similar but vary with regards to several phrases/words. I'd like to build a docstring template and then apply string formatting to it. Below is a clumsy implementation…
Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
3
votes
1 answer

Is there something like C#'s "see cref" in Python ReST docstrings?

I'm converting some C# code to Python 3, including documentation which is typically written as XML summaries in the original C# code. Those summaries have references to class names as a element or
Ray
  • 7,940
  • 7
  • 58
  • 90
3
votes
1 answer

How to get the docstring of a function into a variable?

None of these commands will retrieve the docstring of a function and assign it to a variable. How can it be achieved? I attempted various things. One of which is the help function, but it seems to activate an entire program as opposed to returning a…
abc
  • 3,223
  • 1
  • 15
  • 15
3
votes
0 answers

Docstring format for R reference classes

I have a specific question, but then a "teach a man to fish" question... I am writing an R package using Reference Classes. As described here, I am placing docstrings within each method, which roxygen then picks up and places into the class'…
Eric
  • 1,691
  • 16
  • 24
3
votes
0 answers

How to include documentation from various files into Sphinx/Read The Docs documentation?

I just can't seem to figure that out. I have this kind of library basically (simplified): Library |doc | scripts | make | makefiles | R | R scripts | snakemake | snakefiles | config I want comments (docstring or some…
rioualen
  • 948
  • 8
  • 17
3
votes
1 answer

Python documentation style for instances of class vs class objects

What is the standard way to document the type of function arguments and class attributes to differentiate between those that are expected to be instances of a class and those that are expected to be class objects themselves? class…
Shobhit
  • 773
  • 7
  • 20
3
votes
2 answers

Where to add __author__ in Python Packages

I'm writing a simple Python package with (currently) only one module. Where should I put the docstring and __author__ meta attributes? Do they go in the __init__.py or in each modules? Is there a PEP pertaining to this? I was only able to find the…
Milk
  • 2,469
  • 5
  • 31
  • 54
3
votes
1 answer

Programmatically changing docstring of a class object

I have a function and class decorator that changes them to singletons but the docstrings are kind of dropped. Transforming function returns a class object that has its own docstring which should be overwritten. How can I work around this? def…
ArekBulski
  • 4,520
  • 4
  • 39
  • 61
3
votes
1 answer

Python docstrings are unexpectedly None

When I create a class with method like this: class Foo: """ A class """ def bar(): " Hello world " pass I would expect the __doc__ to return the first statement of the method, since both are strings. Unfortunately, this does…
Herbert
  • 5,279
  • 5
  • 44
  • 69
3
votes
1 answer

Python __doc__ returning None

Just recently I have been getting very a bizarre issue, which I have found no resolve to. For example, if I run the following code in the interpreter >>> def test(): ... 'docstring' ... ... ... >>> print(test.__doc__) I get 'docstring,' no…
Cubli
  • 39
  • 5