Docstring

A docstring is a string literal that annotates an associated section of source code. It provides for the same utility as a comment, but unlike a comment is a string literal and is retained as part of the running program. Some development tools display docstring information as part of an interactive help system.

Programming languages that support docstring include Python, Lisp, Elixir, Clojure,[1] Gherkin,[2] Julia[3] and Haskell.[4] Tools that leverage docstring text include cobra-doc (Cobra), doctest (Python), Pydoc (Python), Sphinx (Python).

Examples

Elixir

Documentation is supported at language level, in the form of docstrings. Markdown is Elixir's de facto markup language 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, a docstring is known as a documentation string. The Common Lisp standard states that a particular implementation may choose to discard docstrings. When they are kept, docstrings may be viewed and changed using the DOCUMENTATION function.[5] For instance:

 (defun foo () "hi there" nil)
 (documentation #'foo 'function) => "hi there"

Python

In Python, a docstring is a string literal that follows a module, class or function definition. It must be nothing but a string literal, not any other kind of expression. The docstring is accessible via the associated code element's __doc__ attribute and the help function.

The following Python code declares docstrings for each program element:

"""The module's docstring"""

class MyClass:
    """The class's docstring"""

    def my_method(self):
        """The method's docstring"""

If saved as mymodule.py, 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

See also

References

  1. ^ "Function definition with docstring in Clojure". Archived from the original on 2013-01-29. Retrieved 2017-09-20.
  2. ^ "Step Arguments - Doc Strings". Archived from the original on 2016-01-31. Retrieved 2016-06-22.
  3. ^ "Documentation — Julia Language 0.4.1 documentation". docs.julialang.org. Archived from the original on 2015-11-17.
  4. ^ "Docstrings".
  5. ^ CLHS: Standard Generic Function DOCUMENTATION...

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.