How to get doc strings in Python?



Doc strings is the documentation string which is string literal, and it occurs in the class, module, function or method definition, and it is written as a first statement.

There are two types of a Doc strings:

  • one-line Doc strings
  • multi-line Doc strings

These are mainly used in DataScience /Machine Learning Programming.

one-line Doc strings

This type of Doc strings fit  in one line. You can write them  either using single quotes( ' ...  ') or triple quotes ('''....''') . 


Example 1

in the following program we are declaring a doc string with in a function, and printing its contents.

def square(x):
'''Returns argument x is squared.'''
return x**x

print (square.__doc__)
help(square)

Output

Returns argument x is squared.
Help on function square in module __main__:

square(x)
Returns argument x is squared.


Multi-line Doc strings

The Multi-line Doc Strings is similar to  One-line Doc Strings the only difference  is, after the first  line of the multi-line doc strings we leave a single blank line, followed by descriptive text before closing it.

Example 2


def function(arg1):
"""Explanation of the function

Parameters:
arg1 (int): Description of arg1

Returns:
int:Returning value


"""

return arg1

print(function.__doc__)

Output

 As you can observe that the sentence "Explanation of the function"  is separated from other contents of the comment  by a single blank line.

Explanation of the function

Parameters:
arg1 (int): Description of arg1

Returns:
int:Returning value

 

raja
Sri
Published on 25-Jun-2019 16:44:43
Advertisements