Easy Tutorial
❮ Zookeeper Setup Es6 Concise Tutorial ❯

Python DocStrings

Category Programming Techniques

DocStrings are an essential tool for documenting code, making your program documentation clearer and more understandable.

You can define a docstring by using a pair of three single quotes ''' or a pair of three double quotes """ at the first line of a function body.

You can call the docstring attribute of a function using __doc__ (note the double underscores).

#!/usr/bin/python
# -*- coding: UTF-8 -*-

def function():
        ''' say something here!
        '''
        pass

print (function.__doc__) # Call doc

Output:

say something here!

DocStrings convention: The first line briefly describes the function's purpose, the second line is blank, and the third line provides a detailed description of the function.

#!/usr/bin/python
# -*- coding: UTF-8 -*-

def printMax(x,y):
    '''Prints the maximum of two numbers.

    Both values must be integers.'''
    x=int(x)
    y=int(y)
    if x>y:
        print(x,'is the maximum')
    else:
        print(y,'is the maximum')

printMax(3,5)
print (printMax.__doc__) # Call doc

Output:

5 is the maximum
Prints the maximum of two numbers.

    Both values must be integers.

** Share My Notes

Cancel

-

-

-

❮ Zookeeper Setup Es6 Concise Tutorial ❯