get list of callable methods

J

John Hunter

What is the best way to get a list of all functions actually defined
in a module, ignoring those functions that the module may have
imported but not defined?

The following gets a list of all the callables, but includes functions
that some.module imports

funcs = []
for o in dir(some.module):
if o.startswith('_'): continue
p = getattr(some.module, o)
if not callable(p): continue
funcs.append(p)




JDH
 
L

Lonnie Princehouse

You could check to see if the module's source file matches the
function's source file:

# returns
# True if func was defined in mod's file,
# False if not _or_ if source file isn't known

def f_defined_by_module(func, mod):
try:
return func.func_code.co_filename == mod.__file__
except:
return False

# Return functions defined in mod
def module_defined_functions(mod):
return [f for f in mod.__dict__.itervalues() if callable(f) and \
f_defined_by_module(f, mod)]


But then again, there are plenty of ways to create modules or
functions that don't even have a source file...
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top