showing help(M) when running module M standalone

tiissa tiissa at nonfree.fr
Sat Jul 30 11:46:27 EDT 2005


Chris wrote:
> hello,
> I have a small module which only contains some utility functions. when 
> running this standalone I would like to show the module docs and each 
> function docs, as if doing
> 
>     import M
>     help(M)
> 
> I came up with the following but I reckon there is a much simpler way?
> 
> if __name__ == '__main__':
>     print __doc__
>     print "\nFUNCTIONS:\n"
>     for x in __all__:
>         print x
>         exec "print " + x + ".__doc__"
> 
> Works but does not seem right using exec for such a task.

One way would be to use the locals() [1] to get rid of the exec:

if __name__ == '__main__':
     print __doc__
     print "\nFUNCTIONS:\n"
     for x in __all__:
         print x
         print locals()[x].__doc__


However, if you just want to call help, calling it on '__main__' seems 
to work:

if __name__ == '__main__':
     help(__name__)


[1] http://docs.python.org/lib/built-in-funcs.html



More information about the Python-list mailing list