|
| 1 | +# Python Documentation |
| 2 | +Documenting Python code is pretty straightforward! It is broken up into two components: comments, and docstrings. |
| 3 | + |
| 4 | +___ |
| 5 | + |
| 6 | +## Docstrings |
| 7 | +Python docstrings go by a few main formats, here are the resources for each of those! |
| 8 | + |
| 9 | +### *reStructuredText - PEP 287* |
| 10 | +reStructuredText (reST for short) is a docstring format that leverages the popular plain-text syntax language known as, well, reST. reST ***is not*** exclusively used for Python. This is great, because docstrings written use it gain the ability to be used in the vast ecosystem of reST tools! |
| 11 | +- [CheatSheet](https://github.com/ralsina/rst-cheatsheet/blob/master/rst-cheatsheet.rst) |
| 12 | +- [Spec](https://www.python.org/dev/peps/pep-0287/) |
| 13 | + |
| 14 | +Example ([thanks, Trelent](https://trelent.net)): |
| 15 | +```python |
| 16 | +def mult_nums(nums): |
| 17 | + """ |
| 18 | + Multiplies all the numbers in a list of numbers. |
| 19 | +
|
| 20 | + :author: Generated by Trelent |
| 21 | + :param nums: The list of numbers to multiply. |
| 22 | + :type nums: list |
| 23 | + :returns: The product of all the numbers in the list. |
| 24 | + :rtype: int |
| 25 | + """ |
| 26 | + result = 1 |
| 27 | + for num in nums: |
| 28 | + result *= num |
| 29 | + return result |
| 30 | +``` |
| 31 | + |
| 32 | +### *Google* |
| 33 | +The Google format is a python docstring format style that is widely used, but which originated as a part of Google's internal Python styling guide. It is extremely human-readable, and emphasizes this over support for automated tooling. |
| 34 | +- [CheatSheet](https://cheatography.com/sunnyphiladelphia/cheat-sheets/google-style-for-python/) |
| 35 | +- [Spec](https://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings) |
| 36 | + |
| 37 | +Example ([thanks, Sphinx](https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html)): |
| 38 | +```python |
| 39 | +def func(arg1, arg2): |
| 40 | + """Summary line. |
| 41 | +
|
| 42 | + Extended description of function. |
| 43 | +
|
| 44 | + Args: |
| 45 | + arg1 (int): Description of arg1 |
| 46 | + arg2 (str): Description of arg2 |
| 47 | +
|
| 48 | + Returns: |
| 49 | + bool: Description of return value |
| 50 | +
|
| 51 | + """ |
| 52 | + return True |
| 53 | +``` |
| 54 | + |
| 55 | +### *NumPy Docstring Standard* |
| 56 | +The NumPy Docstring standard is a documentation format standardized by the popular open source math library, NumPy. It is also extremely human-readable, and emphasizes this over support for automated tooling. |
| 57 | +- [Spec](https://numpydoc.readthedocs.io/en/latest/format.html#docstring-standard) |
| 58 | + |
| 59 | +Example ([thanks, Sphinx](https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html)): |
| 60 | +```python |
| 61 | +def func(arg1, arg2): |
| 62 | + """Summary line. |
| 63 | +
|
| 64 | + Extended description of function. |
| 65 | +
|
| 66 | + Parameters |
| 67 | + ---------- |
| 68 | + arg1 : int |
| 69 | + Description of arg1 |
| 70 | + arg2 : str |
| 71 | + Description of arg2 |
| 72 | +
|
| 73 | + Returns |
| 74 | + ------- |
| 75 | + bool |
| 76 | + Description of return value |
| 77 | +
|
| 78 | + """ |
| 79 | + return True |
| 80 | +``` |
| 81 | +___ |
| 82 | + |
| 83 | +## Comments |
| 84 | +Code commenting is super helpful to other developers when used in moderation. Comments should be primarily used to explain the, *ahem*, intricate components of your code. |
| 85 | + |
| 86 | + |
| 87 | +### The do's and don'ts |
| 88 | +___ |
| 89 | +Python comments are really simple to use! Just prefix your comment with a `#` character like so: |
| 90 | +```python |
| 91 | +# <Your comment goes here> |
| 92 | +``` |
| 93 | + |
| 94 | +A comment can be on its own line: |
| 95 | +```python |
| 96 | +def foo(a,b): |
| 97 | + # This is a comment! |
| 98 | + return a+b |
| 99 | +``` |
| 100 | + |
| 101 | +A comment can follow a piece of code on the same line: |
| 102 | +```python |
| 103 | +def foo(a,b): |
| 104 | + return a+b # This is also a comment! |
| 105 | +``` |
| 106 | + |
| 107 | +Try not to over-use them, docstrings are meant to explain what a broader snippet of code does, not comments! |
| 108 | + |
| 109 | +**Good example**: |
| 110 | +```python |
| 111 | +def foo(a,b): |
| 112 | + """Adds together the numbers a and b""" |
| 113 | + return a+b |
| 114 | +``` |
| 115 | + |
| 116 | +**Bad example**: |
| 117 | +```python |
| 118 | +def foo(a,b): |
| 119 | + # Start with our variables a and b, and add them together |
| 120 | + c = a+b |
| 121 | + |
| 122 | + # Great, now return the computed sum as c |
| 123 | + return c |
| 124 | +``` |
| 125 | +___ |
0 commit comments