module for printing all the methods in a class, with inheritance

J.Jacob joost_jacob at hotmail.com
Mon Apr 29 10:32:42 EDT 2002


Sometimes when using a class from a 3rd party library I wonder what
all the methods are.  Sometimes you need list of all the methods for
a class, not just the ones the class defines but also the ones it
inherits.  Module showclasses.py below tries to do just that, and
shows where the methods are inherited from.  

I have a Windows batchfile in c:\WINNT containing the line:
python -c "import showclasses; showclasses.dumpmodule('%1')"
so i can do
C:\mydirectory\anywhere> showclasses SocketServer
when i am interested in the methods in all the classes available
after you import the 'SocketServer' module.
A *n.x batchfile would contain the same line, but with '$1' instead
of '%1'.
Of course you can also just dump all the methods in a class, for
example when you are unsure about names when coding, use dump(ClassName).

The showclasses module is not perfect but sometimes it helps me.  It
could be easily extended to also print attributes but i learned that
often manipulating attributes is evil.


Questions:

* If you do dumpmodule('copy'), copy is a standard python module, you
will see that the Error class declared in the copy module cannot be
dumped.  Also you will see a class with the name C that is declared
locally in the _test function, it should not be listed.  How to
improve this?

* Now with the new 2.2 Python version classes is this still a good
approach?  Unfortunately i do not have the 2.2 version myself.  Any
solution should be backwards compatible to python 1.5.2.  But i do
like the new developments in Python, it's just that i have to use
1.5.2 too often. 

* Please let me know if you see improvements or if it does not work
with your Python installation.


Sourcecode:

------------------  showclasses.py  ---------------------------------
import sys, pyclbr, string

def dumpmethods(c, classname=None):
    methods = c.methods.items()
    methods.sort(lambda a, b: cmp(a[1], b[1]))
    for method, lineno in methods:
        print "  def " + method,
        if classname:
            print " "*(20-len(method)),
            print "(" + classname + ")",
        print
    if classname and c.super:
        for s in c.super: dumpmethods(s, classname=s.name)


def dump(c):    # adapted from Fredrik Lundh: Python Standard Library
    try:
        s = "class " + c.name   # class header
        if c.super:
            s = s + "(" + string.join(
              map(lambda v: v.name, c.super), ", ") + ")"
        print s + ":"
        # print method names, sorted by line number
        dumpmethods(c)
        # print inherited method names, sorted by line number
        for s in c.super: dumpmethods(s, classname=s.name)
    except AttributeError:
        print 'Cannot dump the class', c
    print

def dumpmodule(m):
    try:
        mod = pyclbr.readmodule(m)
    except ImportError:
        print 'No module named', m, 'in your Python installation',
        sys.exit(1)
    for k, v in mod.items():
        dump(v)

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print "Usage: $ python "+sys.argv[0]+" module_name"
        sys.exit(1)
    dumpmodule(sys.argv[1])
---------------------------------------------------------------------

Joost Jacob
www.liacs.nl/home/jjacob/

I was gratified to be able to answer promptly, and I did.  I said I
didn't know.
                -- Mark Twain

<groundcontrol-to-redhat--please-upgrade-2-2-2-'ly-yrs>



More information about the Python-list mailing list