471,119 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,119 software developers and data experts.

How to list all functions in an imported module?

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?
Jul 18 '05 #1
6 1892
In article <88**************************@posting.google.com >,
kl*******@home.com (Kamilche) wrote:
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)"
Jul 18 '05 #2
On Thu, 2004-05-27 at 08:06, Kamilche wrote:
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
Jul 18 '05 #3
kl*******@home.com (Kamilche) wrote in message news:<88**************************@posting.google. com>...
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
import os,types
for k,v in os.__dict__.items(): .... 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'>
_exit : <type 'builtin_function_or_method'>
rmdir : <type 'builtin_function_or_method'>


Regards
Peter
Jul 18 '05 #4
Peter Abel wrote:
kl*******@home.com (Kamilche) wrote in message news:<88**************************@posting.google. com>...
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

import os,types
for k,v in os.__dict__.items():


... 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
Jul 18 '05 #5
Peter Hansen <pe***@engcorp.com> wrote in
news:o-********************@powergate.ca:
Peter Abel wrote:
kl*******@home.com (Kamilche) wrote in message
news:<88**************************@posting.google. com>...
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

>import os,types
>for k,v in os.__dict__.items():


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


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:
FUNCTION_TYPES = (types.BuiltinFunctionType, types.BuiltinMethodType,
types.FunctionType,
types.MethodType,)
for k,v in os.__dict__.items():

if isinstance(v, FUNCTION_TYPES):
print '%-20s: %r' % (k,type(v))
Jul 18 '05 #6
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
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Blair Hall | last post: by
2 posts views Thread by Aubrey Hutchison | last post: by
1 post views Thread by John Hunter | last post: by
3 posts views Thread by Philippe C. Martin | last post: by
2 posts views Thread by Brian | last post: by
16 posts views Thread by devicerandom | last post: by
5 posts views Thread by Matt_D | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.