HOME

TheInfoList



OR:

In programming, a docstring is a
string literal string literal or anonymous string is a literal for a string value in the source code of a computer program. Modern programming languages commonly use a quoted sequence of characters, formally "bracketed delimiters", as in x = "foo", where , "foo ...
specified in
source code In computing, source code, or simply code or source, is a plain text computer program written in a programming language. A programmer writes the human readable source code to control the behavior of a computer. Since a computer, at base, only ...
that is used, like a comment, to document a specific segment of code. Unlike conventional source code comments, or even specifically formatted comments like docblocks, docstrings are not stripped from the source tree when it is parsed and are retained throughout the runtime of the program. This allows the programmer to inspect these comments at run time, for instance as an interactive help system, or as
metadata Metadata (or metainformation) is "data that provides information about other data", but not the content of the data itself, such as the text of a message or the image itself. There are many distinct types of metadata, including: * Descriptive ...
. Languages that support docstrings include Python,
Lisp Lisp (historically LISP, an abbreviation of "list processing") is a family of programming languages with a long history and a distinctive, fully parenthesized Polish notation#Explanation, prefix notation. Originally specified in the late 1950s, ...
,
Elixir An elixir is a sweet liquid used for medical purposes, to be taken orally and intended to cure one's illness. When used as a dosage form, pharmaceutical preparation, an elixir contains at least one active ingredient designed to be taken orall ...
,
Clojure Clojure (, like ''closure'') is a dynamic programming language, dynamic and functional programming, functional dialect (computing), dialect of the programming language Lisp (programming language), Lisp on the Java (software platform), Java platfo ...
,
Gherkin A pickled cucumber – commonly known as a pickle in the United States, Canada and Australia and a gherkin ( ) in Britain, Ireland, South Africa, and New Zealand – is a usually small or miniature cucumber that has been pickled in a brine, ...
, Julia and
Haskell Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
.


Implementation examples


Elixir

Documentation is supported at language level, in the form of docstrings.
Markdown Markdown is a lightweight markup language for creating formatted text using a plain-text editor. John Gruber created Markdown in 2004 as an easy-to-read markup language. Markdown is widely used for blogging and instant messaging, and also used ...
is Elixir's de facto
markup language A markup language is a Encoding, text-encoding system which specifies the structure and formatting of a document and potentially the relationships among its parts. Markup can control the display of a document or enrich its content to facilitate au ...
of choice for use in docstrings: def module MyModule do @moduledoc """ Documentation for my module. With **formatting**. """ @doc "Hello" def world do "World" end end


Lisp

In Lisp, docstrings are known as documentation strings. The
Common Lisp Common Lisp (CL) is a dialect of the Lisp programming language, published in American National Standards Institute (ANSI) standard document ''ANSI INCITS 226-1994 (S2018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperli ...
standard states that a particular implementation may choose to discard docstrings whenever they want, for whatever reason. When they are kept, docstrings may be viewed and changed using the DOCUMENTATION function.CLHS: Standard Generic Function DOCUMENTATION...
/ref> For instance: (defun foo () "hi there" nil) (documentation #'foo 'function) => "hi there"


Python

The common practice of documenting a code object at the head of its definition is captured by the addition of docstring syntax in the Python language. The docstring for a Python code object (a module, class, or function) is the first statement of that code object, immediately following the definition (the 'def' or 'class' statement). The statement must be a bare string literal, not any other kind of expression. The docstring for the code object is available on that code object's __doc__ attribute and through the help function. The following Python file shows the declaration of docstrings within a Python source file: """The module's docstring""" class MyClass: """The class's docstring""" def my_method(self): """The method's docstring""" def my_function(): """The function's docstring""" Assuming that the above code was saved as , the following is an interactive session showing how the docstrings may be accessed: >>> import mymodule >>> help(mymodule) The module's docstring >>> help(mymodule.MyClass) The class's docstring >>> help(mymodule.MyClass.my_method) The method's docstring >>> help(mymodule.my_function) The function's docstring >>>


Tools using docstrings

* cobra -doc (Cobra) *
doctest doctest is a module included in the Python programming language's standard library that allows the easy generation of tests based on output from the standard Python interpreter shell, cut and pasted into docstrings. Implementation specifics Doct ...
(Python) *
Pydoc Pydoc is the standard documentation module for the programming language Python. Similar to the functionality of Perldoc within Perl and Javadoc within Java, Pydoc allows Python programmers to access Python's documentation help files, generate tex ...
(Python) *
Sphinx A sphinx ( ; , ; or sphinges ) is a mythical creature with the head of a human, the body of a lion, and the wings of an eagle. In Culture of Greece, Greek tradition, the sphinx is a treacherous and merciless being with the head of a woman, th ...
(Python)


See also

*
Literate programming Literate programming (LP) is a programming paradigm introduced in 1984 by Donald Knuth in which a computer program is given as an explanation of how it works in a natural language, such as English, interspersed (embedded) with snippets of macr ...
– alternative code commenting paradigm *
Plain Old Documentation Plain Old Documentation (pod) is a lightweight markup language used to document the Perl programming language as well as Perl modules and programs. Design Pod is designed to be a simple, clean language with just enough syntax to be useful. It p ...
– Perl documentation


References

{{Reflist


External links


Python Docstrings
at Epydoc's
SourceForge SourceForge is a web service founded by Geoffrey B. Jeffery, Tim Perdue, and Drew Streib in November 1999. SourceForge provides a centralized software discovery platform, including an online platform for managing and hosting open-source soft ...
page
Documentation in GNU Emacs Lisp
from the
doxygen Doxygen ( ) is a documentation generator that works with many programming languages. It extracts information from specially-formatted source code comments and saves the information in one of various supported formats. Doxygen supports static ...
documentation about Python docstrings Programming constructs Lisp (programming language) Python (programming language) Source code documentation formats String (computer science)