Skip to content

Commit 66e8678

Browse files
committed
python: Documentation on docstrings was added
1 parent d2ab44a commit 66e8678

File tree

2 files changed

+223
-0
lines changed

2 files changed

+223
-0
lines changed

docs/python/docstrings.md

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
---
2+
id: docstrings
3+
title: Convention for docstrings
4+
sidebar_label: Docstrings
5+
---
6+
7+
## Introduction
8+
9+
> Code is more often read than written - Guido Van Rossum
10+
11+
A few lines of description written on top of a function can save
12+
the coder or a future developer hours or time reading the code.
13+
14+
**Docstrings** are documentation that is written along with the code.
15+
There are different typs of docstrings in Python:
16+
17+
- Class
18+
- Class methods
19+
- Package
20+
- Modules
21+
- Functions
22+
- Script
23+
24+
25+
### Comments are not docstrings
26+
27+
While comments are also written alongside code, docstrings are different
28+
from comments. While comments start with a '#' symbol, docstrings are
29+
also enclosed withing triple double quotes """This is a docstring""".
30+
The placement of docstrings is also crucial. Docstrings placed arbitrarily
31+
may simply be construed as a comment
32+
33+
To illustrate this try the following in the python console
34+
35+
```python3
36+
class Test:
37+
"""This is a class docstring
38+
"""
39+
40+
def example_method():
41+
"""This is a method docstring
42+
"""
43+
pass
44+
45+
def example_method_2():
46+
# This is a comment
47+
pass
48+
```
49+
50+
51+
```python3
52+
> print(Test.__doc___)
53+
This is a class docstring
54+
>
55+
> print(Test.example_method.__doc__)
56+
This is a method docstring
57+
>
58+
> print(Test.example_method_2.__doc__
59+
None
60+
```
61+
62+
As you can see from the examples above, docstrings get
63+
attached to the `__doc__` property of the code itself whereas,
64+
the comments do not
65+
66+
67+
### Usage of docstrings
68+
69+
From the console, you can use docstrings to an overview of
70+
code as follows
71+
72+
```python3
73+
> help(Test)
74+
Help on class Test in module __main__:
75+
76+
class Test(builtins.object)
77+
| This is a class docstring
78+
|
79+
| Methods defined here:
80+
|
81+
| example_method()
82+
| This is a method docstring
83+
|
84+
| example_method_2()
85+
86+
87+
```
88+
89+
If a docstring is provided, you can get more readable
90+
information about python code
91+
92+
Furthermore, there are tools that can take this to the
93+
next level by creating a static website of documentation
94+
for your code:
95+
96+
- [Sphinx](http://www.sphinx-doc.org/en/stable/)
97+
- [Epydoc](http://epydoc.sourceforge.net/)
98+
- [Read the docs](https://readthedocs.org/)
99+
- [Doxygen](http://www.stack.nl/~dimitri/doxygen/manual/docblocks.html#pythonblocks)
100+
- [MkDocs](https://www.mkdocs.org/)
101+
- [Pycco](https://pycco-docs.github.io/pycco/)
102+
103+
104+
## Conventions
105+
106+
The following sections describe the conventions that are recommended by Leapfrog
107+
108+
### Python's official standard
109+
110+
The PEPs are considered to be the official standard for Python. The following sections
111+
talk specifically about docstring and we recommend that you read them
112+
113+
- [PEP-256 -- Docstring Conventions](https://www.python.org/dev/peps/pep-0257/#specification) (**Must Read**)
114+
- [PEP-8 Comments Section](https://www.python.org/dev/peps/pep-0008/#comments)
115+
- [PEP 287 reStructedText Docstring Format](https://www.python.org/dev/peps/pep-0287/)
116+
117+
### Docstring template selection
118+
119+
While you can write anything between the triple quotes(""") to
120+
write your docstring, it is generally recommended to follow a
121+
template for consistency and also for libraries to be able to
122+
parse your docstring easily.
123+
124+
The official documentation standard for Python is:
125+
126+
- [reStructured Text Docstrings](http://docutils.sourceforge.net/rst.html) (**Recommended**)
127+
128+
Note that the above was recommended by [PEP 287](https://www.python.org/dev/peps/pep-0287/)
129+
130+
If you're using using Numpy related libraries, it is better
131+
to use
132+
133+
- [Numpy Docstrings](https://numpydoc.readthedocs.io/en/latest/format.html) (**Recommended for AI projects**)
134+
135+
136+
Other docstring templates are also available but it is highly
137+
recommended that you follow the ones above:
138+
139+
- [Google docstrings](https://github.com/google/styleguide/blob/gh-pages/pyguide.md#38-comments-and-docstrings)
140+
- [Epytext](http://epydoc.sourceforge.net/epytext.html)
141+
142+
143+
### Where to add docstrings
144+
145+
The developers should add docstrings in the following locations
146+
147+
- At the start of every Python file
148+
- At the beginning of every class
149+
- After each function declaration
150+
- At the beginning of the `__init__.py` file for Module/Package documentation
151+
152+
### What to not miss
153+
154+
Use the documentation template of your choice and try not to miss the following
155+
in the docstrings
156+
157+
- A brief description of the entity that is being documented (**Mandatory**)
158+
- Follow the above by above by examples and implementation details (**Recommended**)
159+
- Add typing information where things can get confusing (**Recommended**). As python is a
160+
dynamically typed language, adding some type definition to the documentation can save
161+
the developers a lot of debugging time
162+
- Autodeploy the documentation site using a static deployment tool
163+
to check that your docstrings render correctly
164+
165+
166+
## Examples
167+
168+
Here are few examples of docstrings to get you started
169+
170+
```python3
171+
"""The method below prints a given string twice
172+
173+
The print method has been called twice for
174+
implementing this method
175+
176+
:param param1: String that is to be printed
177+
:type param1: str
178+
:return: Length of the input string
179+
:rtype: int
180+
"""
181+
def print_twice(param1):
182+
print(param1)
183+
print(param1)
184+
185+
return len(param1)
186+
```
187+
188+
The following shows type definition on the same line.
189+
190+
```python3
191+
"""The method below prints a given string twice
192+
193+
The print method has been called twice for
194+
implementing this method
195+
196+
:param str param1: String that is to be printed
197+
:return: Length of the input string
198+
:rtype: int
199+
"""
200+
def print_twice(param1):
201+
print(param1)
202+
print(param1)
203+
204+
return len(param1)
205+
```
206+
207+
## References
208+
209+
Thanks to the following
210+
211+
212+
- [Pycharm Documentation on Docstrings](https://www.jetbrains.com/help/pycharm/using-docstrings-to-specify-types.html)
213+
- [Documenting Python Code by RealPython](https://realpython.com/documenting-python-code/#documenting-your-python-projects)
214+
- [PEP-256 -- Docstring Conventions](https://www.python.org/dev/peps/pep-0257/#specification)
215+
- [PEP-8 Comments Section](https://www.python.org/dev/peps/pep-0008/#comments)
216+
- [Documentation - Python Guide](https://docs.python-guide.org/writing/documentation/)
217+
- [Documenting in Python - DevGuide](https://devguide.python.org/documenting/)
218+
- [daouzli - stackoverflow](https://stackoverflow.com/questions/3898572/what-is-the-standard-python-docstring-format)
219+
220+
221+
222+

sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports =
1515
"python/classes"
1616
]
1717
},
18+
"python/docstrings",
1819
"python/tools",
1920
"python/general",
2021
"python/exceptions",

0 commit comments

Comments
 (0)