Post

Python Docstring

In this tutorial, we will learn about python docstring, where and why python docstrings are used.

Python Docstring
πŸš€ Find Better AI Tools Faster Submit Your AI Tool & Reach Thousands of Builders Get Started β†’

What is Python Docstring?

Python docstring or Documentation strings is a string literally used in the class, module, function, or method definition.

As like multiline comment, docstring is also declared using three (β€˜β€™β€™) or four (β€œβ€β€). For example

β€˜β€™β€™ triple single quotes β€˜β€™β€™ or β€œβ€β€ triple double quotes ”””

Docstrings are accessible from the doc attribute (__doc__) for any of the Python objects and built-in functions. Docstrings are great for understanding the functionality of the more extensive code of the project.

Example of Code:

1
2
3
4
5
def addition(n):
		''' This is a docstrings example we have added in addition function '''
		return n+n

print(addition.__doc__)

To run this docstring code, we have to follow this step.

1
2
print(addition.__doc__)

Here the output of string literal.

Output

1
2
	This is a docstrings example we have added in the addition function. 

Here, we have documented our addition function, and then we are accessing it with __doc__ attribute.

Docstring in built-in functions

Now let’s use docstring for the built-in python function and let it have a print function, for example.

1
2
print(print.__doc__)

Output:

1
2
3
4
5
6
7
8
9
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file:  a file-like object (stream); defaults to the current sys.stdout.
sep:   string inserted between values, default a space.
end:   string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.

As we can see, we got the documentation output of the print() function defined by python.

Docstring in Python Module

1
2
3
import numpy 
print(numpy.__doc__)

Output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
NumPy
=====

Provides
  1. An array object of arbitrary homogeneous items
  2. Fast mathematical operations over arrays
  3. Linear Algebra, Fourier Transforms, Random Number Generation

How to use the documentation
----------------------------
Documentation is available in two forms: docstrings provided
with the code, and a loose standing reference guide, available from
`the NumPy homepage <https://www.scipy.org>`_.

We recommend exploring the docstrings using
`IPython <https://ipython.org>`_, an advanced Python shell with
TAB-completion and introspection capabilities.  See below for further
instructions.

The docstring examples assume that `numpy` has been imported as `np`::

  >>> import numpy as np

.
.
.
.

Khushal Jethava
Khushal Jethava

Machine Learning Engineer at Codiste, specializing in Generative AI, NLP, and Computer Vision. Building production AI systems with Python.

πŸš€ Find Better AI Tools Faster Submit Your AI Tool & Reach Thousands of Builders Get Started β†’
This post is licensed under CC BY 4.0 by the author.
πŸš€ Find Better AI Tools Faster Submit Your AI Tool & Reach Thousands of Builders Get Started β†’
πŸš€ Find Better AI Tools Faster Submit Your AI Tool & Reach Thousands of Builders Get Started β†’