SNIP IT: inspectObject(thing)

Phlip phlip_cpp at yahoo.com
Mon Jun 3 04:24:11 CEST 2002


Hop Nyh:

Because groups.google.com is the world's ultimate backup server, I'm 
parking this snippet here. Given most object references, it prints out an 
"instant man page" of the value of every member, the name of every local or 
inherited method, and every __doc__ string of every method.

Some of it could be replaced by calls into the inspect.py module, and one 
could extend it using that module. Put another way I wrote this before I 
learned of inspect.py.

def printDocString(thing):
    dok = None

    try:
        dok = thing.__doc__
    except: pass  #  whatEver

    if dok == None:
        dok = ''

    print '"'+dok+'"'

    return


def _showAllMethods(klassList):
    for klass in klassList:
        for z, funk in klass.__dict__.items():
            print klass.__name__+"."+z,
            printDocString(funk)

        _showAllMethods(klass.__bases__)


#
# prints out the dir() for any object in a pretty format.
#
def inspectObject(obj):
    printDocString(obj)
    klass = obj.__class__

    try:
        print "("+klass.__name__+")"
    except(AttributeError):
        pass

    printDocString(klass)

    # TODO: call inspectObject on each member?

    for z in dir(obj):
        print "."+z, repr(getattr(obj, z))

    _showAllMethods([klass])
    return

-- 
  Phlip
       http://www.greencheese.org/LucidScheming
  --  Just think: Four billion people in the
      world have never received Spam...  --




More information about the Python-list mailing list