How to list all functions in an imported module?

K

Kamilche

I can't figure out how to list all functions from an imported module.
I searched Google, but all the answers I found didn't work. Did
something change in Python 2.2, perhaps there's a new method of doing
it?
 
M

Mark McEahern

I can't figure out how to list all functions from an imported module.
I searched Google, but all the answers I found didn't work. Did
something change in Python 2.2, perhaps there's a new method of doing
it?

Try the inspect module.

// m
 
R

Roy Smith

I can't figure out how to list all functions from an imported module.
I searched Google, but all the answers I found didn't work. Did
something change in Python 2.2, perhaps there's a new method of doing
it?

I would start with "dir (moduleName)"
 
P

Peter Abel

I can't figure out how to list all functions from an imported module.
I searched Google, but all the answers I found didn't work. Did
something change in Python 2.2, perhaps there's a new method of doing
it?

e.g the module os
.... if type(v) == types.BuiltinFunctionType or\
.... type(v) == types.BuiltinMethodType or\
.... type(v) == types.FunctionType or\
.... type(v) == types.MethodType:
.... print '%-20s: %r' % (k,type(v))
....
rename : <type 'builtin_function_or_method'
lseek : <type 'builtin_function_or_method'>
_get_exports_list : <type 'function'>
execle : <type 'function'>
chmod : <type 'builtin_function_or_method'>
execlp : <type 'function'>
open : <type 'builtin_function_or_method'>
write : <type 'builtin_function_or_method'>
putenv : <type 'builtin_function_or_method'>
fdopen : <type 'builtin_function_or_method'>
_pickle_statvfs_result: <type 'function'>
startfile : <type 'builtin_function_or_method'>
umask : <type 'builtin_function_or_method'>
system : <type 'builtin_function_or_method'>
_execvpe : <type 'function'>
getpid : <type 'builtin_function_or_method'>
tmpnam : <type 'builtin_function_or_method'>
dup : <type 'builtin_function_or_method'>
spawnve : <type 'builtin_function_or_method'>
getenv : <type 'function'>
isatty : <type 'builtin_function_or_method'>
execvpe : <type 'function'>
dup2 : <type 'builtin_function_or_method'>
read : <type 'builtin_function_or_method'>
execvp : <type 'function'>
popen3 : <type 'builtin_function_or_method'>
_make_stat_result : <type 'function'>
execve : <type 'builtin_function_or_method'>
utime : <type 'builtin_function_or_method'>
execl : <type 'function'>
chdir : <type 'builtin_function_or_method'>
renames : <type 'function'>
strerror : <type 'builtin_function_or_method'>
remove : <type 'builtin_function_or_method'>
fstat : <type 'builtin_function_or_method'>
execv : <type 'builtin_function_or_method'>
execlpe : <type 'function'>
tempnam : <type 'builtin_function_or_method'>
tmpfile : <type 'builtin_function_or_method'>
popen4 : <type 'builtin_function_or_method'>
popen2 : <type 'builtin_function_or_method'>
stat : <type 'builtin_function_or_method'>
abort : <type 'builtin_function_or_method'>
close : <type 'builtin_function_or_method'>
_exists : <type 'function'>
spawnl : <type 'function'>
makedirs : <type 'function'>
access : <type 'builtin_function_or_method'>
unsetenv : <type 'function'>
mkdir : <type 'builtin_function_or_method'>
spawnv : <type 'builtin_function_or_method'>
listdir : <type 'builtin_function_or_method'>
_pickle_stat_result : <type 'function'>
lstat : <type 'builtin_function_or_method'>
spawnle : <type 'function'>
getcwd : <type 'builtin_function_or_method'>
unlink : <type 'builtin_function_or_method'>
_make_statvfs_result: <type 'function'>
popen : <type 'builtin_function_or_method'>
times : <type 'builtin_function_or_method'>
pipe : <type 'builtin_function_or_method'>
removedirs : <type 'function'>

Regards
Peter
 
P

Peter Hansen

Peter said:
e.g the module os



... if type(v) == types.BuiltinFunctionType or\
... type(v) == types.BuiltinMethodType or\
... type(v) == types.FunctionType or\
... type(v) == types.MethodType:
... print '%-20s: %r' % (k,type(v))

Yuck... wouldn't using "callable(v)" be a lot nicer?

-Peter
 
D

Duncan Booth

Yuck... wouldn't using "callable(v)" be a lot nicer?

It depends whether he wants to include callable classes or really does want
just functions.

If he doesn't want callable classes, then a nicer way would be to use
isinstance, which also gives you the option of factoring out the list of
types:
types.BuiltinMethodType,
types.FunctionType,
types.MethodType,)
if isinstance(v, FUNCTION_TYPES):
print '%-20s: %r' % (k,type(v))
 
K

Kamilche

Thanks guys. It DOES work, as you stated... my problem was I had a
module called the same name as a variable I was using, yeesh.

I'm used to C, where you have to declare variables before use, and the
compiler catches stuff like this. I might have to go back to a
Hungarian-like naming conventions for variables, it appears.

--Kamilche
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top