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

Python- lines_startswith code

The following is a code that I must complete. So, the result I want is to read through a file(every line) and if the first letter of the line that i read in the file matches with the letter that I have chosen returning the line in the list matches.…
0
votes
0 answers

how to unwrap lines of a docstring

I am using the module docopt but need to have line-wrapping in the main docstring of my programs. In order to pass this line-wrapped docstring to docopt, I want to 'unwrap' the lines of my docstring in a sensible and robust way such that docopt can…
d3pd
  • 7,935
  • 24
  • 76
  • 127
0
votes
2 answers

Python: Generating HTML output documentation based on Docstring

Is there a way to easily output html documentation based on python docstrings? If there are, how to do this? I am familiar with HTML/CSS so theming the output is not important, but if there are existing themes, they would help. I am hoping for a…
Capuchin
  • 3,465
  • 6
  • 28
  • 40
0
votes
1 answer

Why some docstrings of methods in class are not shown in pydoc?

I am using pydoc to create documentation. But it is only displaying the docstring for a class and for methods such as __init__. Is there any parameter to be passed to pydoc to create a documentation for each method of a class as…
exAres
  • 4,806
  • 16
  • 53
  • 95
0
votes
1 answer

Triple-quoted string literal recognized as docstring undesirably by sphinx

In one of my __init__.py file: description=""" This is a package for blablahblah. It contains blahblah. """ The string variable is just for the purpose of usage in other modules and I don't want it to be recognized by sphinx and extracted out as…
RNA
  • 146,987
  • 15
  • 52
  • 70
0
votes
2 answers

autoattribute for parallel assignment of instance variables

When documenting instance variables, I can do class Foo: def __init__(self): self.spam = 4 """Docstring for instance attribute spam.""" This doesn't work for parallel assignment class Foo: def __init__(self): …
XrXr
  • 2,027
  • 1
  • 14
  • 20
0
votes
1 answer

Python abstract doc comment for method

How to specify for IDE (PyCharm) and for documentation tools, that described method is abstract? What i want: class Test: @abc.abstractmethod def test(self): """ This method must be overridden :abstract """ …
Pavel Patrin
  • 1,630
  • 1
  • 19
  • 33
0
votes
2 answers

How do you get a list of objects in a script so that you can print __doc__ on each of them?

In my script test.py I have a lot of functions and classes and then I have the following: for i in dir(): if i[0] != '_': print(type(i), i.__doc__) But it doesnt work because when using dir() to get a list of what is in my namespace, I…
Paul H
  • 901
  • 2
  • 7
  • 12
0
votes
2 answers

Python helptip change text

I'm working on some basic Python code but I've got a problem I've never been able to solve. Indeed, I would like to change the helptip of a function and I can totally do it. First of all, here is my code (for example) : def main(arg1,arg2,arg3): …
Numerobis
  • 3
  • 3
0
votes
1 answer

Python and JavaScript/AngularJS documentation on the one server?

The advantages of this approach include: Consistent docstring syntax everywhere Centralsied documentation server; find all your docs in one place Search and jump-to-source from any documented function or class; in either language Are there any…
A T
  • 13,008
  • 21
  • 97
  • 158
0
votes
1 answer

Testing a long string with doctest

I need to test the following code using doctest. I has to be doctest and not any other method. I keep getting "Expected: nothing". Is there any way to do this? import pickle #Loading lists from file using the pickle module with…
user2802349
  • 151
  • 1
  • 1
  • 10
0
votes
1 answer

Using Sphinx's autodoc with Zope 2 (docstring issue)

I'd like to use Sphinx to document my Zope 2 Product and it would be nice to also use the autodoc feature, which pulls informations out of docstrings in my modules. Now in Zope the docstrings are unfortunately used to designate a method as…
Georg Pfolz
  • 1,416
  • 2
  • 12
  • 12
0
votes
1 answer

docstring blocks elif statement

Let me past the exact code I have: This is the short module class SentenceSplitter: def __init__(self, filename=None): self._raw_text = self.raw_text(filename) self._sentences = self.to_sentences() def raw_text(self, filename): text =…
dragons
  • 175
  • 1
  • 10
0
votes
2 answers

Is there a way to generate IPython docstrings on the fly?

I like that IPython will fetch docstrings if I type foo.bar? However, I may sometimes build the foo.bar method dynamically, using foo.__getattr__. I could conceivably also generate a docstring dynamically, perhaps in a magic method like…
ChrisB
  • 4,628
  • 7
  • 29
  • 41
0
votes
1 answer

In python, how to print the docstrings of all functions defined in an imported module, without the functions that the imported module itself imported?

The following code prints each function's docstring from an imported module. However, the results incude some functions that were not defined within the module, but rather, were imported by the module. import inspect import my_module all_functions…
sgarza62
  • 5,998
  • 8
  • 49
  • 69