29

I'm trying to use a Python library written in C that has no documentation of any kind. I want to use introspection to at least see what methods and classes are in the modules. Does somebody have a function or library I can use to list the functions (with argument lists) and classes (with methods and member variables) within a module?

I found this article about Python introspection, but I'm pretty sure it doesn't apply to Python 2.5. Thanks for the help.

Randall Cook
  • 6,728
  • 6
  • 33
  • 68
Evan Kroske
  • 4,506
  • 12
  • 40
  • 59

4 Answers4

58

Here are some things you can do at least:

import module

print dir(module) # Find functions of interest.

# For each function of interest:
help(module.interesting_function)
print module.interesting_function.func_defaults
Alexander Ljungberg
  • 6,302
  • 4
  • 31
  • 37
12

Mark Pilgrim's chapter 4, which you mention, does actually apply just fine to Python 2.5 (and any other recent 2.* version, thanks to backwards compatibility). Mark doesn't mention help, but I see other answers do.

One key bit that nobody (including Mark;-) seems to have mentioned is inspect, an excellent module in Python's standard library that really helps with advanced introspection.

corrado
  • 23
  • 1
  • 8
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
8

Just this is pretty good too:

import module
help(module)

It will print the docstring for the module, then list the contents of the module, printing their docstrings too.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • I can not find any alternative way to get the contents of help, particularly the "PACKAGE CONTENTS" is what I'm looking for. This is in the help text, but none of the other answers provide a way to get it algorithmically. – AlanSE Oct 24 '17 at 17:48
  • 1
    ah, found it inside source code for this https://github.com/python/cpython/blob/ac317700ce7439e38a8b420218d9a5035bba92ed/Lib/pydoc.py#L733, with pattern of `for importer, modname, ispkg in pkgutil.iter_modules(object.__path__):` – AlanSE Oct 24 '17 at 18:03
4

The dir() functions shows all members a module has.

Alex Gaynor
  • 14,353
  • 9
  • 63
  • 113