showing help(M) when running module M standalone

C

Chris

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.

any hint would be great!

thanks
chris
 
D

Dan Sommers

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.

So don't use exec.
any hint would be great!

for x in __all__:
print x.__doc__

HTH,
Dan
 
T

tiissa

Chris said:
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
 
T

Thomas Heller

Chris said:
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.

any hint would be great!

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

The only downside is that the module name is printed as '__main__'.

Thomas
 
C

Chris

hello,
thanks for all suggestions,

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

does actually work any even shows the modulename it should.

chris
 
D

Dan Sommers

I tried that, problem is that __all__ containts strings...

Okay, that's twice I've lept before I looked. How about this:

for a in __all__:
print globals()[a].__doc__

HTH,
Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top