Skip to content

Commit 7f3972c

Browse files
committed
docstring tutorial for zaab1t
1 parent d47480d commit 7f3972c

File tree

5 files changed

+232
-2
lines changed

5 files changed

+232
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ to learn more about whatever you want after studying it.
5050
18. [Modules](basics/modules.md)
5151
19. [Exceptions](basics/exceptions.md)
5252
20. [Classes](basics/classes.md)
53+
21. [Docstrings](basics/docstrings.md)
5354

5455
### Advanced
5556

advanced/datatypes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,5 +398,5 @@ star](../README.md#how-can-i-thank-you-for-writing-and-sharing-this-tutorial).
398398
You may use this tutorial freely at your own risk. See
399399
[LICENSE](../LICENSE).
400400

401-
[Previous](../basics/classes.md) | [Next](functions.md) |
401+
[Previous](../basics/docstrings.md) | [Next](functions.md) |
402402
[List of contents](../README.md#advanced)

basics/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ to learn more about whatever you want after studying it.
2626
18. [Modules](modules.md)
2727
19. [Exceptions](exceptions.md)
2828
20. [Classes](classes.md)
29+
21. [Docstrings](docstrings.md)
2930

3031
***
3132

basics/classes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,5 +424,5 @@ star](../README.md#how-can-i-thank-you-for-writing-and-sharing-this-tutorial).
424424
You may use this tutorial freely at your own risk. See
425425
[LICENSE](../LICENSE).
426426

427-
[Previous](exceptions.md) | [Next](../advanced/datatypes.md) |
427+
[Previous](exceptions.md) | [Next](docstrings.md) |
428428
[List of contents](../README.md#basics)

basics/docstrings.md

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
# Help and Docstrings
2+
3+
In this tutorial we have used `help()` a few times. It's great and you
4+
can use it as much as you want to. For example, running `help(str)`
5+
displays a nice list of all string methods and explanations of what they
6+
do, and `help(list.extend)` explains what extending something to a list
7+
does.
8+
9+
You can get help of many other things too. For example:
10+
11+
```python
12+
>>> stuff = []
13+
>>> help(stuff.append)
14+
Help on built-in function append:
15+
16+
append(object, /) method of builtins.list instance
17+
Append object to the end of the list.
18+
19+
>>> help(print)
20+
Help on built-in function print in module builtins:
21+
22+
print(...)
23+
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
24+
25+
Prints the values to a stream, or to sys.stdout by default.
26+
Optional keyword arguments:
27+
...
28+
```
29+
30+
## Docstrings
31+
32+
Let's see what happens if we [define a function](defining-functions.md)
33+
and call `help()` on that.
34+
35+
```python
36+
>>> def thing(stuff):
37+
... return stuff * 2
38+
...
39+
>>> help(thing)
40+
Help on function thing in module __main__:
41+
42+
thing(stuff)
43+
>>>
44+
```
45+
46+
That sucked! We have no idea about what it does based on this. All we
47+
know is that it takes a `thing` argument.
48+
49+
This is when documentation strings or docstrings come in. All we need to
50+
do is to add a string to the beginning of our function and it will show
51+
up in `help(the_function)`. Like this:
52+
53+
```python
54+
>>> def thing(stuff):
55+
... "hello there"
56+
... return stuff * 2
57+
...
58+
>>> help(thing)
59+
Help on function thing in module __main__:
60+
61+
thing(stuff)
62+
hello there
63+
```
64+
65+
Note that docstrings are not comments. If you add a `# comment` to the
66+
beginning of the function it won't show up in `help()`.
67+
68+
## Multi-line strings
69+
70+
When we did `help(print)`, we got more than one line of help. Maybe we
71+
could do that in our own docstring too?
72+
73+
```python
74+
>>> def thing():
75+
... "This thing does stuff.\n\nIt always returns None."
76+
...
77+
>>> help(thing)
78+
Help on function thing in module __main__:
79+
80+
thing()
81+
This thing does stuff.
82+
83+
It always returns None.
84+
>>>
85+
```
86+
87+
That's better, but how what if we want to do 5 lines of prints? Our
88+
`"stuff\n\nstuff\nstuff"` thing would be really long and hard to work
89+
with. But Python has multi-line strings too. They work like this:
90+
91+
```python
92+
>>> """bla bla bla
93+
...
94+
... bla bla
95+
... bla bla bla"""
96+
'bla bla bla\n\nbla bla\nbla bla bla'
97+
>>>
98+
```
99+
100+
So we can write documented functions like this:
101+
102+
```python
103+
>>> def thing():
104+
... """This thing does stuff.
105+
...
106+
... It always returns None.
107+
... """
108+
...
109+
>>> help(thing)
110+
Help on function thing in module __main__:
111+
112+
thing()
113+
This thing does stuff.
114+
115+
It always returns None.
116+
117+
>>>
118+
```
119+
120+
It's recommended to always use `"""strings like this"""` for docstrings,
121+
even if the docstring is only one line long. This way it's easy to add
122+
more stuff to it later.
123+
124+
## Documenting other stuff
125+
126+
Docstrings aren't actually limited to functions. You can use them for
127+
documenting [classes](classes.md) and their methods too. For example,
128+
let's make a file like this and save it to `test.py`:
129+
130+
```python
131+
"""A test module.
132+
133+
It contains a class and a function.
134+
"""
135+
136+
137+
class Thing:
138+
"""This is a test class."""
139+
140+
def thingy(self):
141+
"""This is a test method."""
142+
print("hello")
143+
144+
145+
def do_hello():
146+
"""This is a test function."""
147+
thing = Thing()
148+
thing.thingy()
149+
```
150+
151+
Then we can import it and call help on it:
152+
153+
```python
154+
>>> import test
155+
>>> help(test)
156+
Help on module testie:
157+
158+
NAME
159+
testie - A test module.
160+
161+
DESCRIPTION
162+
It contains a class and a function.
163+
164+
CLASSES
165+
builtins.object
166+
Thing
167+
168+
class Thing(builtins.object)
169+
| This is a test class.
170+
|
171+
| Methods defined here:
172+
|
173+
| thingy(self)
174+
| This is a test method.
175+
|
176+
| ----------------------------------------------------------------------
177+
| Data descriptors defined here:
178+
|
179+
| __dict__
180+
| dictionary for instance variables (if defined)
181+
|
182+
| __weakref__
183+
| list of weak references to the object (if defined)
184+
185+
FUNCTIONS
186+
do_hello()
187+
This is a test function.
188+
189+
FILE
190+
/home/akuli/testie.py
191+
```
192+
193+
That's pretty cool. We just added docstrings to our code and Python made
194+
this thing out of it.
195+
196+
You might be wondering what `__weakref__` is. You don't need to care
197+
about it, and I think it would be better if `help()` would hide it.
198+
199+
## When should we use docstrings?
200+
201+
Always use docstrings when writing code that other people will import.
202+
The `help()` function is awesome, so it's important to make sure it's
203+
actually helpful.
204+
205+
If your code is not meant to be imported, docstrings are usually a good
206+
idea anyway. Other people reading your code will understand what it's
207+
doing without having to read through all of the code.
208+
209+
## Summary
210+
211+
- `help()` is awesome.
212+
- A `"""triple-quoted string"""` string in the beginning of a function,
213+
class or file is a docstring. It shows up in `help()`.
214+
- Docstrings are not comments.
215+
- Usually it's a good idea to add docstrings everywhere
216+
217+
***
218+
219+
If you have trouble with this tutorial please [tell me about
220+
it](../contact-me.md) and I'll make this tutorial better. If you
221+
like this tutorial, please [give it a
222+
star](../README.md#how-can-i-thank-you-for-writing-and-sharing-this-tutorial).
223+
224+
You may use this tutorial freely at your own risk. See
225+
[LICENSE](../LICENSE).
226+
227+
[Previous](classes.md) | [Next](../advanced/datatypes.md) |
228+
[List of contents](../README.md#basics)

0 commit comments

Comments
 (0)