En Mon, 29 Dec 2008 05:26:52 -0200, member Basu <b...@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
--
http://mail.python.org/mailman/listinfo/python-list