Get a list of functions in a file
Chris Rebert
clp at rebertia.com
Mon Dec 29 04:50:07 EST 2008
On Sun, Dec 28, 2008 at 11:26 PM, member Basu <basu at archlinux.us> 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.
Assuming you've already imported the module as 'mod':
func_names = [name for name in dir(mod) if callable(getattr(mod, name))]
funcs = [getattr(mod, name) for name in dir(mod) if
callable(getattr(mod, name))]
Note that such lists will also include classes (as they too are
callable). There are ways of excluding classes (and other objects that
implement __call__), but it makes the code a bit more complicated.
Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
More information about the Python-list
mailing list