Get a list of functions in a file

Terry Reedy tjreedy at udel.edu
Mon Dec 29 20:20:38 CET 2008


member Basu wrote:
> I'm putting some utility functions in a file and then building a simple 
> shell interface to them. Is their some way I can automatically get a 
> list of all the functions in the file? I could wrap them in a class and 
> then use attributes, but I'd rather leave them as simple functions.

Lets assume that either
1) You only define functions (bind function names) in the module, or
2) You start any other top-level names with '_' so that they do not get 
imported.

import utilfuncs
funcs = vars(utilfuncs) # dict of name:func pairs
names = funcs.keys()

# display names and ask user to select 'inputname'
# then, assuming no args

output = funcs[inputname]()

tjr





More information about the Python-list mailing list