{"id":60,"date":"2024-01-24T14:36:10","date_gmt":"2024-01-24T14:36:10","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=60"},"modified":"2024-01-24T14:36:10","modified_gmt":"2024-01-24T14:36:10","slug":"python-function-docstrings","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/24\/python-function-docstrings\/","title":{"rendered":"Python Function Docstrings"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about how to use docstrings to add documentation to a function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the help() function<\/h2>\n\n\n\n<p>Python provides a built-in function called\u00a0<code>help()<\/code>\u00a0that allows you to show the documentation of a\u00a0function.<\/p>\n\n\n\n<p>The following example shows the documentation of the\u00a0<code>print()<\/code>\u00a0function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>help(print)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(...) print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream. <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Note that you can use the\u00a0<code>help()<\/code>\u00a0function to show the documentation of\u00a0modules,\u00a0classes,\u00a0functions, and keywords. This tutorial focuses on function documentation only.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using docstrings to document functions<\/h2>\n\n\n\n<p>To document your functions, you can use docstrings. The\u00a0PEP 257\u00a0provides the docstring conventions.<\/p>\n\n\n\n<p>When the first line in the function body is a string, Python will interpret it as a docstring. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def add(a, b): \"Return the sum of two arguments\" return a + b<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>And you can use the\u00a0<code>help()<\/code>\u00a0function to find the documentation of the\u00a0<code>add()<\/code>\u00a0function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>help(add)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>add(a, b) Return the sum of two arguments<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Typically, you use multi-line docstrings:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def add(a, b): \"\"\" Add two arguments Arguments: a: an integer b: an integer Returns: The sum of the two arguments \"\"\" return a + b<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>add(a, b) Add the two arguments Arguments: a: an integer b: an integer Returns: The sum of the two arguments <\/code><small>Code language: Shell Session (shell)<\/small><\/code><\/pre>\n\n\n\n<p>Python stores the docstrings in the&nbsp;<code>__doc__<\/code>&nbsp;property of the function.<\/p>\n\n\n\n<p>The following example shows how to access the&nbsp;<code>__doc__<\/code>&nbsp;property of the&nbsp;<code>add()<\/code>&nbsp;function:<code>add.__doc__<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about how to use docstrings to add documentation to a function. Introduction to the help() function Python provides a built-in function called\u00a0help()\u00a0that allows you to show the documentation of a\u00a0function. The following example shows the documentation of the\u00a0print()\u00a0function: Output: Note that you can use the\u00a0help()\u00a0function to show the documentation [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-60","post","type-post","status-publish","format-standard","hentry","category-4-functions"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/60","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=60"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/60\/revisions"}],"predecessor-version":[{"id":61,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/60\/revisions\/61"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=60"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=60"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}