introspection

B

brad tilley

How do I import a module and then ask it to show me its methods or other
aspects about itself during execution? I'd like to do something such as
this:

import win32api

print win32api.methods()

I'd like to write some test scripts that load modules and probe them for
information about themselves.
 
J

John Machin

brad said:
How do I import a module and then ask it to show me its methods or other
aspects about itself during execution? I'd like to do something such as
this:

import win32api

print win32api.methods()

I'd like to write some test scripts that load modules and probe them for
information about themselves.

Hi Brad,

You might like to look at the built-in inspect module.

Cheers,
John
 
B

Ben Finney

brad tilley said:
How do I import a module and then ask it to show me its methods or other
aspects about itself during execution? I'd like to do something such as
this:

import win32api

print win32api.methods()

I'd like to write some test scripts that load modules and probe them
for information about themselves.

As well as the 'inspect' module, you may be able to get some distance
just with builtin functionality::
['DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE',
'S', 'U', 'UNICODE', 'VERBOSE', 'X', '__all__', '__builtins__',
'__doc__', '__file__', '__name__', 'compile', 'engine', 'error',
'escape', 'findall', 'finditer', 'match', 'purge', 'search',
'split', 'sub', 'subn', 'template']
>>> [(n, getattr(re, n)) for n in dir(re)]
[('DOTALL', 16), ('I', 2), ('IGNORECASE', 2), ('L', 4), ('LOCALE',
4), ('M', 8), ('MULTILINE', 8), ('S', 16), ('U', 32), ('UNICODE',
32), ('VERBOSE', 64), ('X', 64), ('__all__', ['match', 'search',
'sub', 'subn', 'split', 'findall', 'compile', 'purge', 'template',
'escape', 'I', 'L', 'M', 'S', 'X', 'U', 'IGNORECASE', 'LOCALE',
'MULTILINE', 'DOTALL', 'VERBOSE', 'UNICODE', 'error',
'finditer']),
[...]
]
>>> [n for n in dir(re) if hasattr(getattr(re, n), '__call__')]
['compile', 'escape', 'findall', 'finditer', 'match', 'purge',
'search', 'split', 'sub', 'subn', 'template']
 
F

Fredrik Lundh

brad said:
How do I import a module and then ask it to show me its methods or other
aspects about itself during execution? I'd like to do something such as
this:

import win32api

dir(win32api)
help(win32api)

and also

import inspect
help(inspect)

</F>
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top