[Tutor] all methods in a module

Kent Johnson kent37 at tds.net
Fri May 27 19:47:41 CEST 2005


Bob Gailer wrote:
>> 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.

eval() is not required, getattr() is a better solution as I showed in my previous reply.

> 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__]

For the general case it would be prudent to say
    if callable(object) and hasattr(object, '__doc__') and object.__doc__
so a missing docstring doesn't terminate the loop.

Kent



More information about the Tutor mailing list