@@ -44,7 +44,7 @@ thing(stuff)
4444```
4545
4646That sucked! We have no idea about what it does based on this. All we
47- know is that it takes a ` thing ` argument.
47+ know is that it takes a ` stuff ` argument.
4848
4949This is when documentation strings or docstrings come in. All we need to
5050do is to add a string to the beginning of our function and it will show
@@ -198,11 +198,143 @@ this thing out of it.
198198You might be wondering what ` __weakref__ ` is. You don't need to care
199199about it, and I think it would be better if ` help() ` would hide it.
200200
201+ ## Popular Docstring Formats
202+
203+ There are different styles for writing docstrings. If you are contributing to
204+ another Python project, make sure to use the same style as rest of that project
205+ is using.
206+
207+ If you are starting a new project, then you can use whichever style you
208+ want, but don't "reinvent the wheel"; use an existing style instead instead of
209+ making up your own. Here are some examples of popular docstring styles to choose
210+ from:
211+
212+ ### Sphinx Style
213+
214+ [ Sphinx] ( https://www.sphinx-doc.org/en/master/ ) is the Python documentation tool
215+ that [ the official Python documentation] ( https://docs.python.org/3/ ) uses.
216+ By default, sphinx expects you to write docstrings like this:
217+
218+ ``` python
219+ class Vehicles :
220+ """
221+ The Vehicles object contains lots of vehicles.
222+ :param arg: The arg is used for ...
223+ :type arg: str
224+ :ivar arg: This is where we store arg
225+ :vartype arg: str
226+ """
227+
228+ def __init__ (self , arg ):
229+ self .arg = arg
230+
231+ def cars (self , distance , destination ):
232+ """ We can't travel a certain distance in vehicles without fuels, so here's the fuels
233+
234+ :param distance: The amount of distance traveled
235+ :type amount: int
236+ :param bool destinationReached: Should the fuels be refilled to cover required distance?
237+ :raises: :class:`RuntimeError`: Out of fuel
238+
239+ :returns: A Car mileage
240+ :rtype: Cars
241+ """
242+ ...
243+ ```
244+
245+ ### Google Style
246+
247+ Google Style is meant to be easier to read and use without a tool like sphinx.
248+ Sphinx can be configured to use that with
249+ [ sphinx.ext.napoleon] ( https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html ) .
250+
251+ ``` python
252+ class Vehicles :
253+ """
254+ The Vehicles object contains lots of vehicles.
255+
256+ Args:
257+ arg (str): The arg is used for...
258+
259+ Attributes:
260+ arg (str): This is where we store arg.
261+ """
262+
263+ def __init__ (self , arg ):
264+ self .arg = arg
265+
266+ def cars (self , distance , destination ):
267+ """ We can't travel distance in vehicles without fuels, so here is the fuels
268+
269+ Args:
270+ distance (int): The amount of distance traveled
271+ destination (bool): Should the fuels refilled to cover the distance?
272+
273+ Raises:
274+ RuntimeError: Out of fuel
275+
276+ Returns:
277+ cars: A car mileage
278+ """
279+ ...
280+
281+ ```
282+
283+ ### Numpy Style
284+
285+ [ Numpy] ( https://numpy.org/ ) is a large and popular Python library,
286+ and numpy developers have their own docstring style.
287+
288+ ``` python
289+ class Vehicles :
290+ """
291+ The Vehicles object contains lots of vehicles.
292+
293+ Parameters
294+ ----------
295+ arg : str
296+ The arg is used for ...
297+ *args
298+ The variable arguments are used for ...
299+ **kwargs
300+ The keyword arguments are used for ...
301+
302+ Attributes
303+ ----------
304+ arg : str
305+ This is where we store arg.
306+ """
307+
308+ def __init__ (self , arg ):
309+ self .arg = arg
310+
311+ def cars (self , distance , destination ):
312+ """ We can't travel distance in vehicles without fuels, so here is the fuels
313+
314+ Parameters
315+ ----------
316+ distance : int
317+ The amount of distance traveled
318+ destination : bool
319+ Should the fuels refilled to cover the distance?
320+
321+ Raises
322+ ------
323+ RuntimeError
324+ Out of fuel
325+
326+ Returns
327+ -------
328+ cars
329+ A car mileage
330+ """
331+ pass
332+ ```
333+
201334## When should we use docstrings?
202335
203- Always use docstrings when writing code that other people will import.
204- The ` help() ` function is awesome, so it's important to make sure it's
205- actually helpful.
336+ I recommend using docstrings when writing code that other people will import.
337+ The ` help() ` function is awesome, so it's good to make sure it's actually helpful.
206338
207339If your code is not meant to be imported, docstrings are usually a good
208340idea anyway. Other people reading your code will understand what it's
@@ -214,7 +346,7 @@ doing without having to read through all of the code.
214346- A ` """triple-quoted string""" ` string in the beginning of a function,
215347 class or file is a docstring. It shows up in ` help() ` .
216348- Docstrings are not comments.
217- - Usually it's a good idea to add docstrings everywhere
349+ - Usually it's a good idea to add docstrings everywhere.
218350
219351***
220352
0 commit comments