Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Get a list of functions in a file

3 views
Skip to first unread message

Chris Rebert

unread,
Dec 29, 2008, 4:50:07 AM12/29/08
to member Basu, pytho...@python.org
On Sun, Dec 28, 2008 at 11:26 PM, member Basu <ba...@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

Gabriel Genellina

unread,
Dec 29, 2008, 4:51:58 AM12/29/08
to pytho...@python.org
En Mon, 29 Dec 2008 05:26:52 -0200, member Basu <ba...@archlinux.us>
escribió:

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

Such file is called a "module" in Python. Just import it (I'll use glob as
an example, it's a module in the standard library, look for glob.py). To
get all names defined inside the module, use the dir() function:

py> import glob
py> dir(glob)
['__all__', '__builtins__', '__doc__', '__file__', '__name__',
'__package__', 'f
nmatch', 'glob', 'glob0', 'glob1', 'has_magic', 'iglob', 'magic_check',
'os', 'r
e', 'sys']

Note that you get the names of all functions defined inside the module
(fnmatch, glob, glob0, has_magic...) but also many other names (like os,
re, sys that are just imported modules, and __all__, __doc__, etc that are
special attributes)
If you are only interested in functions, the best way is to use inspect:

py> import inspect
py> inspect.getmembers(glob, inspect.isfunction)
[('glob', <function glob at 0x00B807B0>), ('glob0', <function glob0 at
0x00B80AF
0>), ('glob1', <function glob1 at 0x00B80AB0>), ('has_magic', <function
has_magi
c at 0x00B80B30>), ('iglob', <function iglob at 0x00B80A70>)]

Modules are covered in the Python tutorial here
<http://docs.python.org/tutorial/modules.html> and the inspect module is
documented here <http://docs.python.org/library/inspect.html>


--
Gabriel Genellina

Aaron Brady

unread,
Dec 29, 2008, 8:40:42 AM12/29/08
to
On Dec 29, 3:50 am, "Chris Rebert" <c...@rebertia.com> wrote:

> On Sun, Dec 28, 2008 at 11:26 PM, member Basu <b...@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.

No, not in general. It's a weakness of one of the strengths of
Python. For instance, if you define a function in a string, or return
one from another function, there's no way to get at it.

If you do want to import it, you can put any executable code inside an
'if __name__== "__main__"' block, so it won't get executed while
you're trying to index/catalog it.

If you're interested in something more hard-core, you might like the
'tokenize' module. And I think the pattern you're looking for is
'every "def" outside a string, and even some in one.'

P.S. Did not receive the original message on Google Groups.

Terry Reedy

unread,
Dec 29, 2008, 2:20:38 PM12/29/08
to pytho...@python.org
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


0 new messages