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

What differentiates a keyword argument from a non-keyword argument in Python?

I am trying to understand the difference between non-keyword arguments an keyword arguments. It seems to me that every argument may be also used as a keyword argument. def print_employee_details(name, age=0, location=None): """Print details of…
Lone Learner
  • 18,088
  • 20
  • 102
  • 200
3
votes
1 answer

Why does a decorated class looses its docstrings?

I'm currently implementing an API on which I need to decorate the class Wrapper, but I want it to keep its docstring to make it available to the API user. Take a look at the following minimal working example : class ClassDecorator: …
Felipe Aguirre
  • 218
  • 1
  • 12
3
votes
1 answer

Python IDLE won't show docstring

My IDLE (Python 3.4.3) won't show functions doc-strings when typing the name of the function. Is anybody familiar with this problem? I've tried everything, including uninstall etc. Answers on the web are nowhere to be found. I'm talking about…
almog6564
  • 31
  • 2
3
votes
1 answer

Adding help text for Python module in Docstring

I am trying to add help text to function in my python script, similar to when parentheses are opened for input() or print(). Docstrings do something similar, but isn't useful when writing the code. See below picture for what I want. The yellow pop…
L0neGamer
  • 275
  • 3
  • 13
3
votes
1 answer

Indicating keyworded-ness of arguments in NumpyDoc DocStrings?

In trying to follow the NumpyDoc format for my DocStrings, I can't seem to figure out how to tell the user that an argument is a keyword argument (ie. specified as SomeFunc(theKeyWordArg=100) as opposed to SomeFunc(100) ). The documentation I've…
Demis
  • 5,278
  • 4
  • 23
  • 34
3
votes
1 answer

Simplifying class docstring in python

I have the most simplest class derived from Exception, looks something like this: class Test(Exception): ''' My Test class ''' def __init__(self, param=None): self.param = param def __str__(self): return 'Test…
rep_movsd
  • 6,675
  • 4
  • 30
  • 34
3
votes
1 answer

How can text in the options configuration of Docopt be wrapped?

I have a few detailed option specifications in the docstring used for configuration of Docopt. Some of the items are quite lengthy. Is there a way of wrapping the text to make it more legible or to make it fit to a line width more easily? Let's say…
d3pd
  • 7,935
  • 24
  • 76
  • 127
3
votes
3 answers

Python: Why does this doc test fail?

This code that's in the doctest works when run by itself, but in this doctest it fails in 10 places. I can't figure out why it does though. The following is the entire module: class requireparams(object): """ >>> @requireparams(['name',…
orokusaki
  • 55,146
  • 59
  • 179
  • 257
3
votes
0 answers

Can I use Sphinx to programmatically access parsed documentation?

I can use docutils to parse reStructuredText formatted docstrings, but it doesn't parse the directives themselves that belong to the Python domain. For example, :type path: unicode gets parsed as: type path
Márcio
  • 677
  • 8
  • 15
3
votes
1 answer

Django admindoc not rendering reStructuredText

Im using django admindocs for documentation and the basic functionality works nicely (I can access the doc pages, models are listed an documented, help_text is included, etc.). Unfortunately, reStructuredText markup in docstrings is completely…
OBu
  • 4,977
  • 3
  • 29
  • 45
3
votes
1 answer

Python doc strings print \n instead of a new line (Python 2.7.5 on a Mac)

So here is what I am typing >>> list = [1,2,3] >>> list.__doc__ "list() -> new empty list\nlist(iterable) -> new list initialized from iterable's items" The problem being that it literally prints "\n" instead of printing a new line, making more…
sicklybeans
  • 299
  • 3
  • 11
3
votes
1 answer

Ruby inline documentation

In IRB, or other interactive interpreter such as pry, how can I get some inline documentation on objects and methods? For example, I can get this far: [1] pry(main)> x = 'hello world' => "hello world" [2] pry(main)> x.st x.start_with? x.strip …
wim
  • 338,267
  • 99
  • 616
  • 750
3
votes
1 answer

Avoid docstrings from parent, in Sphinx

I use Sphinx for autodocs, but I find it annoying how it by default appends the parent class docstring to my docstring. The result is that for each and every documented test class inheriting from unittest.TestCase, I get the docstring "Create an…
Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
3
votes
3 answers

Python IDE with autocomplete mechanism parsing docstring

so far, I only found PyCharm as being able to parse docstring in order to determine a variable's type and enable autocompletion for that variable, e.g.: def func(arg): """ epydoc style docstring @type arg: str @rtype: str """ …
bruno
  • 69
  • 5
3
votes
1 answer

What is the string following the "def" on Python? (not the docstring)

I am watching videos of CS262 on Udacity and I am confused by this kind of procedure definitions: def t_WORD(token): r'[^ <>]+' # any reg. exp. ruleset is placed here # ... more processing # ... more processing return token This…
Phil
  • 13,875
  • 21
  • 81
  • 126