[Tutor] importance of Docstring

Alan Gauld alan.gauld at btinternet.com
Thu Apr 16 23:14:13 CEST 2009

"mbikinyi brat" <mbikinyi_brat at yahoo.com> wrote 

> What is really the importance of Docstring in Python???

Some really great comments about comments, however the 
significance of docstrings beyond normal comments is that 
tools like help() work with them


>>> def f(x):
 '''f(x) -> x+5'''
 return x+5

>>> help(f)
Help on function f in module __main__:

    f(x) -> x+5


So immediately help() knows about f and can read its docstring.
This is a huge help when using other peoples modules, or even 
your own modules that you haven't worked on for a while. You 
can import them and read the doctrings wiothout actually opening 
the code in an editor (which you would need to do for normal 

So they are not essential but they do have a very real use 
beyond simple comments..

I say a bit more about docstrings in my tutorial topic 
"Add a little style"

Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list