get list of callable methods

Lonnie Princehouse finite.automaton at gmail.com
Thu Oct 14 18:31:48 EDT 2004


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...




John Hunter <jdhunter at ace.bsd.uchicago.edu> wrote in message news:<mailman.4965.1097778895.5135.python-list at python.org>...
> 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



More information about the Python-list mailing list