printing class methods

Jason Orendorff jason at jorendorff.com
Fri Jan 25 06:28:36 CET 2002


Joe wrote:
> sorry, just as I saw my mail disappear I remebered -
> dir(class_or_module_name), but the second question still stands - Is there
> a way to find out the number and type of arguments?

Arguments aren't typed.

The "inspect" module will help you find the number and names
of arguments.
  http://www.python.org/doc/current/lib/inspect-classes-functions.html


Sample usage:

import inspect

for name in dir(MyClass):
    method = getattr(MyClass, name)
    if callable(method):
        # methods aren't exactly the same thing as functions,
        # so you get a method, you have to grab it's "im_func"
        # attribute to get at the underlying function.
        try:
            method = method.im_func
        except AttributeError:
            pass
        print name, inspect.getargspec(method)

## Jason Orendorff    http://www.jorendorff.com/





More information about the Python-list mailing list