introspection
Ben Finney
bignose+hates-spam at benfinney.id.au
Thu Aug 31 22:51:41 EDT 2006
brad tilley <rtilley at vt.edu> writes:
> 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::
>>> import re
>>> dir(re)
['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']
--
\ "Madness is rare in individuals, but in groups, parties, |
`\ nations and ages it is the rule." -- Friedrich Nietzsche |
_o__) |
Ben Finney
More information about the Python-list
mailing list