iteration over methods

Troy Melhase troy at
Tue Jan 7 23:26:52 CET 2003

i agree with you that inspect.getmembers would be useful for the OP, but 
it's important to note that inspect.getmembers is actually doing a dir() 
for the caller.  from the source:

def getmembers(object, predicate=None):
     """Return all members of an object as (name, value) pairs
        sorted by name. Optionally, only return members that
        satisfy a given predicate."""
     results = []
     for key in dir(object):
         value = getattr(object, key)
         if not predicate or predicate(value):
             results.append((key, value))
     return results

- troy

Giles Brown wrote:
> As an alternative to dir() you could use the inspect module:
> import inspect
> class Someclass:
>      def __init__(self):
>          pass
>      def method1(self):
>          print "called method1"
>      def method2(self):
>          print "called method2"
>      def runallmethods(self):
>          for name, method in inspect.getmembers(self, inspect.ismethod):
>              if name[:6] == 'method':
>                  method.im_func(self)

More information about the Python-list mailing list