How to list all functions in an imported module?
Duncan Booth
me at privacy.net
Fri May 28 04:10:45 EDT 2004
Peter Hansen <peter at engcorp.com> wrote in
news:o-idnZ6O7qVg5SvdRVn-tA at powergate.ca:
> Peter Abel wrote:
>
>> klachemin at home.com (Kamilche) wrote in message
>> news:<889cbba0.0405270506.3cd91d26 at posting.google.com>...
>>
>>>I can't figure out how to list all functions from an imported module.
>>>I searched Google, but all the answers I found didn't work. Did
>>>something change in Python 2.2, perhaps there's a new method of doing
>>>it?
>>
>>
>> e.g the module os
>>
>>
>>>>>import os,types
>>>>>for k,v in os.__dict__.items():
>>
>> ... if type(v) == types.BuiltinFunctionType or\
>> ... type(v) == types.BuiltinMethodType or\
>> ... type(v) == types.FunctionType or\
>> ... type(v) == types.MethodType:
>> ... print '%-20s: %r' % (k,type(v))
>
> Yuck... wouldn't using "callable(v)" be a lot nicer?
It depends whether he wants to include callable classes or really does want
just functions.
If he doesn't want callable classes, then a nicer way would be to use
isinstance, which also gives you the option of factoring out the list of
types:
>>> FUNCTION_TYPES = (types.BuiltinFunctionType,
types.BuiltinMethodType,
types.FunctionType,
types.MethodType,)
>>> for k,v in os.__dict__.items():
if isinstance(v, FUNCTION_TYPES):
print '%-20s: %r' % (k,type(v))
More information about the Python-list
mailing list