[Tutor] all methods in a module

Bob Gailer bgailer at sbcglobal.net
Fri May 27 17:51:43 CEST 2005


At 04:46 AM 5/27/2005, Johan Meskens CS3 jmcs3 wrote:

>hello
>
> >>> import random
> >>> print random.setstate.__doc__
>Restore internal state from object returned by getstate().
>
>
>my question is
>" how can i loop through all the methods in a module
>   and print out their '__doc__' content ?
>
> >>> for d in dir( random ):
>         print random.???d???.__doc__

The prior responses use dir(), requiring then the use of eval() to get the 
object.
You can get the name and object directly from random.__dict__. The 
following comprehension seems to handle all the stuff in random's dict:

methods = [name, str(object.__doc__)
                   for name, object in random.__dict__.iteritems()
                   if callable(object) and object.__doc__]

Then you can iterate over methods and format/print as desired.

Bob Gailer
mailto:bgailer at alum.rpi.edu
510 558 3275 home
720 938 2625 cell  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050527/1fd23b32/attachment.htm


More information about the Tutor mailing list