You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/write-user-documentation/document-your-code-api-docstrings.md
+10-10Lines changed: 10 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -16,17 +16,17 @@ Package APIs consist of functions, classes, methods and attributes that create a
16
16
17
17
## What is a docstring and how does it relate to documentation?
18
18
19
-
In Python a docstring refers to text in a function, method or class
19
+
In Python, a docstring refers to text in a function, method or class
20
20
that describes what the function does and its inputs and outputs. Python programmers usually refer to the inputs to functions as ["parameters"](https://docs.python.org/3/glossary.html#term-parameter) or ["arguments"](https://docs.python.org/3/faq/programming.html#faq-argument-vs-parameter), and the outputs are often called "return values"
21
21
22
22
The docstring is thus important for:
23
23
24
-
- When you call `help()` in Python, for example, `help(add_numbers)` will show the text of the function's docstring. The docstring thus helps a user better understand how to applying the function more effectively to their workflow.
25
-
- When you build your package's documentation, the docstrings can also be used to automagically create full API documentation that provides a clean view of all its functions, classes, methods, and attributes.
24
+
- When you call `help()` in Python, for example, `help(add_numbers)` will show the text of the function's docstring. The docstring thus helps a user better understand how to apply the function more effectively to their workflow.
25
+
- When you build your package's documentation, the docstrings can also be used to automatically create full API documentation that provides a clean view of all its functions, classes, methods, and attributes.
26
26
27
27
```{tip}
28
28
Example API Documentation for all functions, classes, methods, and attributes in a package.
29
-
* [View example highlevel API documentation for the Verde package. This page lists every function and class in the package along with a brief explanation of what it does](https://www.fatiando.org/verde/latest/api/index.html)
29
+
* [View example high-level API documentation for the Verde package. This page lists every function and class in the package along with a brief explanation of what it does](https://www.fatiando.org/verde/latest/api/index.html)
30
30
* [You can further dig down to see what a specific function does within the package by clicking on an API element](https://www.fatiando.org/verde/latest/api/generated/verde.grid_coordinates.html#verde.grid_coordinates)
31
31
```
32
32
@@ -39,14 +39,14 @@ In Python, this means that you need to add a docstring for
39
39
every user-facing
40
40
class, method, attribute and/or function in your package (_within reason_) that:
41
41
42
-
- Explains what the function, method, attribute or class does
42
+
- Explains what the function, method, attribute, or class does
43
43
- Defines the `type` inputs and outputs (ie. `string`, `int`, `np.array`)
44
44
- Explains the expected output `return` of the object, method or function.
45
45
46
46
(numpy-docstring)=
47
47
### Three Python docstring formats and why we like NumPy style
48
48
49
-
There are several Python docstring formats that you can chose to use when documenting
49
+
There are several Python docstring formats that you can choose to use when documenting
@@ -59,7 +59,7 @@ We suggest using [NumPy-style docstrings](https://numpydoc.readthedocs.io/en/lat
59
59
Python documentation because:
60
60
61
61
- NumPy style docstrings are core to the scientific Python ecosystem and defined in the [NumPy style guide](https://numpydoc.readthedocs.io/en/latest/format.html). Thus you will find them widely used there.
62
-
- The Numpy style docstring is simplified and thus easier-to-read both in the code and when calling `help()` in Python. In contrast, some feel that reST style docstrings is harder to quickly scan, and can take up more lines of code in modules.
62
+
- The Numpy style docstring is simplified and thus easier to read both in the code and when calling `help()` in Python. In contrast, some feel that reST style docstrings are harder to quickly scan, and can take up more lines of code in modules.
63
63
64
64
```{tip}
65
65
If you are using NumPy style docstrings, be sure to include the [sphinx napoleon
@@ -69,10 +69,10 @@ to properly read and format NumPy format docstrings.
69
69
70
70
### Docstring examples Better and Best
71
71
72
-
Below is a good example of a welldocumented function. Notice that this
72
+
Below is a good example of a well-documented function. Notice that this
73
73
function's docstring describes the function's inputs and the function's output
74
74
(or return value). The initial description of the function is short (one line).
75
-
Following that singleline description there is a slightly longer description of
75
+
Following that single-line description, there is a slightly longer description of
76
76
what the function does (2 to 3 sentences). The return of the function is also
77
77
specified.
78
78
@@ -236,7 +236,7 @@ Describing the expected data type that a function or method requires
236
236
helps users better understand how to call a function or method.
237
237
238
238
Type-hints add another layer of type documentation to your code. Type-hints
239
-
make make it easier for new developers, your future self or contributors
239
+
make it easier for new developers, your future self or contributors
240
240
to get to know your code base quickly.
241
241
242
242
Type hints are added to the definition of your function. In the example below, the parameters aNum and aNum2 are defined as being type = int (integer).
0 commit comments