Search code examples
pythonintrospectionpython-module

Get data members in Python module, as help() does


This Python:

data = {}

def f():
    pass

import sys

help(sys.modules[__name__])

Prints this:

Help on built-in module __main__:

NAME
    __main__

FILE
    /foo/bar.py

FUNCTIONS
    f()

DATA
    data = {}

My question is, how can I get a list of "data" objects like the help function does? I looked at inspect.getmembers() but it returns things I don't want, and writing a predicate for it seems like it should not be necessary.


Solution

  • You can read the code behind the help(module) implementation: http://hg.python.org/cpython/file/110b38c36a31/Lib/pydoc.py#l1060

    It's kind of complicated though....