[Doc-SIG] PEP 257 question

Nick Coghlan ncoghlan at gmail.com
Thu Jul 23 14:40:29 CEST 2009


Michael Foord wrote:
> Fred Mailhot wrote:
>> Hi,
>>
>> It is stated in PEP 257 that:
>>
>> "The docstring of a script (a stand-alone program) should be usable as
>> its "usage" message, printed when the script is invoked with incorrect
>> or missing arguments (or perhaps with a "-h" option, for "help").[...]"
> 
> I wasn't aware of that advice. Hmmm...
> 
> Anway - how about this:
> 
> import sys
> module =  sys.modules['__main__'] # or [__name__]
> docstring = module.__doc__

No need for the fancy footwork - remember that a module's globals and
its attributes are the same dict:

~/devel$ cat > demo.py
"My docstring"
print __doc__

~/devel$ python demo.py
My docstring

For modules and classes, the interpreter sets "__doc__" in the current
namespace automatically when it builds the docstring (not functions
though - their docstring isn't added to the local scope).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------


More information about the Doc-SIG mailing list