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

How to avoid redundancy between c++ and boost::python docs?

I'm adding a python module in a c++ code, using boost::python. The c++ project is documented with doxygen. I want to create a documentation for the python module but I don't know how not to be redundant like this : #include using…
cromod
  • 1,721
  • 13
  • 26
4
votes
1 answer

What is the difference between @param and: param in Pycharm

I want to document a class, and I have seen that in PyCharm when a docstring is added automatically it adds :param param_name1: :param param_name2: However, I have seen that some people use @param param_name1: @param param_name2: I wonder which…
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
4
votes
2 answers

Why wasn't the string at the top of this function printed?

I encountered the following function in a tutorial. When I call the function, "This prints a passed string into this function" isn't printed. Why does the function not print this piece of text when called? def printme(str): "This prints a…
user2713650
  • 99
  • 2
  • 8
4
votes
1 answer

Autogenerate python docstrings in Visual Studio Express 2013

Is there a way to automatically generate docstrings for python in Visual Studio Express 2013? If I remember correctly, in PyCharm if you type """ after a function/class header it automatically generates a docstring somewhat like the following: def…
causa prima
  • 1,502
  • 1
  • 14
  • 24
4
votes
1 answer

enforcing python function parameters types from docstring

Both epydoc and Sphinx document generators permit the coder to annotate what the types should be of any/all function parameter. My question is: Is there a way (or module) that enforces these types (at run-time) when documented in the docstring. …
NevilleDNZ
  • 1,269
  • 12
  • 31
4
votes
3 answers

How to have the docstring respect the PEP257, while usable with docopt to comply with i18n using gettext?

According to PEP 257 the docstring of command line script should be its usage message: The docstring of a script (a stand-alone program) should be usable as its "usage" message, printed when the script is invoked with incorrect or missing arguments…
zmo
  • 24,463
  • 4
  • 54
  • 90
4
votes
2 answers

Implementing a class property that preserves the docstring

I have a descriptor that turns a method into a property on the class level: class classproperty(object): def __init__(self, getter): self.getter = getter self.__doc__ = getter.__doc__ def __get__(self, instance, owner): …
javex
  • 7,198
  • 7
  • 41
  • 60
4
votes
2 answers

Create a docstring for an enum for Xcode 5's Quick H⁣elp

I want to document an enum in Xcode 5 so that I can see my docstring as Quick Help, in the autocomplete popup. For example, if I do this: //! Better than a normal int! typedef int superint; then I can type superi and the popup shows superint, and…
4
votes
2 answers

How to add constant string to __doc__ for all class methods?

I have code that looks like this: constString = """ Default docstring info: 1 2 3""" class A(): def A1(): """ First unique docstring. """ pass def A2(): """ Second unique…
Timur Gilmullin
  • 261
  • 1
  • 7
4
votes
1 answer

Python - using decorator.py to preserve a method docstring

I am interested in calling an instance method as both a class method as well as an instance method. This can be done by using the class_or_instance decorator as follows: class class_or_instance(object): def __init__(self, fn): self.fn =…
rk7
  • 65
  • 8
4
votes
1 answer

Python dynamic help and autocomplete generation

I have almost what I want... This dynamic object encapsulating a generic function call with a dynamic docstring generation: def add_docs(tool): def desc(func): func.__doc__ = "Showing help for %s()" % tool return func …
estani
  • 24,254
  • 2
  • 93
  • 76
4
votes
4 answers

Setting the docstring to an expression inside def

I would like to set the func_doc (as an expression) within def. def f(): '''My function help''' #Set the docstring def g(): "My function " + "help" # An expression, so not read as a docstring # can I put something here to set the…
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
4
votes
1 answer

Parse Sphinx like documentation

I have a Sphinx formatted docstring from which I would like to extract the different parts (param, return, type, rtype, etc) for further processing. How can I achieve this?
Hernan
  • 5,811
  • 10
  • 51
  • 86
3
votes
1 answer

Python docstring with vim pythoncomplete is not displaying newlines for my own class functions

I am getting some unexpected results when trying to use Python Omni Completion on my own class functions. The docstring for the functions are not getting formatted correctly with line breaks as shown in the picture below: When I am importing…
veiset
  • 1,973
  • 16
  • 16
3
votes
5 answers

property docstring

Consider the code: class MyClass(object): ''' Keep track of file and its path on disk ''' def __init__(self): self.file = None self.path = None I'd like to add doc-string to all properties. So, I could do something like (for file…
Samvel
  • 182
  • 2
  • 6