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: basics/docstrings.md
+126Lines changed: 126 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -121,6 +121,132 @@ It's recommended to always use `"""strings like this"""` for docstrings,
121
121
even if the docstring is only one line long. This way it's easy to add
122
122
more stuff to it later.
123
123
124
+
## Popular Docstrings Format
125
+
126
+
There might be different documentation strings available. You need not need to worry about the fact that you have to reinvent the wheel to study all. The formats of all the Documentation strings are nearly similar. The patterns are similar, but there are only nitty-gritty changes in each format.
127
+
128
+
##### 1) Sphinx Style :
129
+
130
+
Sphinx is the easy and traditional style, verbose and was initially created specifically for the Python Documentation. Sphinx uses a reStructuredText which is similar in usage to Markdown.
131
+
132
+
```python
133
+
classVehicle(object):
134
+
'''
135
+
The Vehicle object contains lots of vehicles
136
+
:param arg: The arg is used for ...
137
+
:type arg: str
138
+
:param `*args`: The variable arguments are used for ...
139
+
:param `**kwargs`: The keyword arguments are used for ...
140
+
:ivar arg: This is where we store arg
141
+
:vartype arg: str
142
+
'''
143
+
144
+
145
+
def__init__(self, arg, *args, **kwargs):
146
+
self.arg = arg
147
+
148
+
defcars(self, distance, destination):
149
+
'''We can't travel a certain distance in vehicles without fuels, so here's the fuels
150
+
151
+
:param distance: The amount of distance traveled
152
+
:type amount: int
153
+
:param bool destinationReached: Should the fuels be refilled to cover required distance?
154
+
:raises: :class:`RuntimeError`: Out of fuel
155
+
156
+
:returns: A Car mileage
157
+
:rtype: Cars
158
+
'''
159
+
pass
160
+
161
+
```
162
+
163
+
##### 2) Google Style :
164
+
Google Style is easier and more intuitive to use. It can be used for the shorter form of documentation. A configuration of python file needs to be done to get started, so you need to add either sphinx.ext.napoleon or sphinxcontrib.napoleon to the extensions list in conf.py.
165
+
166
+
```python
167
+
classVehicles(object):
168
+
'''
169
+
The Vehicle object contains a lot of vehicles
170
+
171
+
Args:
172
+
arg (str): The arg is used for...
173
+
*args: The variable arguments are used for...
174
+
**kwargs: The keyword arguments are used for...
175
+
176
+
Attributes:
177
+
arg (str): This is where we store arg,
178
+
'''
179
+
180
+
def__init__(self, arg, *args, **kwargs):
181
+
self.arg = arg
182
+
183
+
defcars(self, distance,destination):
184
+
'''We can't travel distance in vehicles without fuels, so here is the fuels
185
+
186
+
Args:
187
+
distance (int): The amount of distance traveled
188
+
destination (bool): Should the fuels refilled to cover the distance?
189
+
190
+
Raises:
191
+
RuntimeError: Out of fuel
192
+
193
+
Returns:
194
+
cars: A car mileage
195
+
'''
196
+
pass
197
+
198
+
```
199
+
200
+
##### 3) Numpy Style :
201
+
202
+
Numpy style has a lot of details in the documentation. It is more verbose than other documentation, but it is an excellent choice if you want to do detailed documentation, i.e., extensive documentation of all the functions and parameters.
203
+
204
+
```python
205
+
classVehicles(object):
206
+
'''
207
+
The Vehicles object contains lots of vehicles
208
+
209
+
Parameters
210
+
----------
211
+
arg : str
212
+
The arg is used for ...
213
+
*args
214
+
The variable arguments are used for ...
215
+
**kwargs
216
+
The keyword arguments are used for ...
217
+
218
+
Attributes
219
+
----------
220
+
arg : str
221
+
This is where we store arg,
222
+
'''
223
+
224
+
def__init__(self, arg, *args, **kwargs):
225
+
self.arg = arg
226
+
227
+
defcars(self, distance, destination):
228
+
'''We can't travel distance in vehicles without fuels, so here is the fuels
229
+
230
+
Parameters
231
+
----------
232
+
distance : int
233
+
The amount of distance traveled
234
+
destination : bool
235
+
Should the fuels refilled to cover the distance?
236
+
237
+
Raises
238
+
------
239
+
RuntimeError
240
+
Out of fuel
241
+
242
+
Returns
243
+
-------
244
+
cars
245
+
A car mileage
246
+
'''
247
+
pass
248
+
```
249
+
124
250
## Documenting other stuff
125
251
126
252
Docstrings aren't actually limited to functions. You can use them for
0 commit comments