Finding all of a classes methods?

Roy Smith roy at panix.com
Sun Feb 3 20:32:30 EST 2002


Is there any way to find all of a class's methods?  I know I can use dir(), 
but that gives you the names of the methods (as strings), not the methods 
themselves (as callable MethodType objects).

I'm building a production system.  Basicly, I want to run every one of a 
class's methods on an object.  The number of methods is large, and will 
grow in the future.  Right now, I'm building a list of the methods as I 
define them.  Something like:

methods = []

def foo (self, x):
   pass
methods.append (foo)

def bar (self, x):
   pass
methods.append (bar)

and so on down the list (yes, the real ones do something more useful than 
pass).  Obviously, the class is keeping track of its methods already.  If I 
could get at that list, I could avoid having to build my own.

Also, I'm slightly mystified about what, exactly, I'm adding to my list.  
When I do:

for method in self.methods:
   method (x)

I get an error saying that the method expected 2 arguments, but only got 1.  
Obviously, it's not getting self because I'm not calling it as a method of 
self.  If I change it to:

for method in self.methods:
   method (self, x)

I get what I want, but I'm not 100% sure that's kosher.



More information about the Python-list mailing list