|
| 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