Friday, 4 December 2015

Python: Documentation strings

Documentation string is a string literal, used to document your python code. Triple quotes(""") are used to represent documentation stings. Doc string occurs as the first statement in a module, function, class, or method definition. You can access the doc string of an object using __doc__ attribute.


test.py
def dummyFunction():
 """Function demonstrates the use of doc strings """
 print("Hello")
 
print(dummyFunction.__doc__)

$ python3 test.py
Function demonstrates the use of doc strings

There are two forms of doc strings.
a.   One-line doc strings
b.   Multi line doc strings

One-line doc strings
One-line doc strings are used to give brief description about a module, function, class etc.,

Multi line doc strings
Multi line doc string contains a summary line followed by blank line, followed by more elaborate description.


test.py
def dummyFunction():
 """Function demonstrates the use of doc strings.
 
  Multi line doc string looks like this.
 """
 print("Hello")
 
print(dummyFunction.__doc__)
$ python3 test.py
Function demonstrates the use of doc strings.
        
                  Multi line doc string looks like this.

Use the command ‘pydoc test’ to generate documentation for test.py. ‘pydoc test’ command generates following documentation.

Help on module test:
 
NAME
    test
 
FILE
    /Users/harikrishna_gurram/study1/python/examples/test.py
 
FUNCTIONS
    dummyFunction()
        Function demonstrates the use of doc strings.
       
        Multi line doc string looks like this.
 


References

Previous                                                 Next                                                 Home

No comments:

Post a Comment