Finding all of a classes methods?

Barry Drake bldrake1 at yahoo.com
Mon Feb 4 01:35:29 EST 2002


Roy Smith <roy at panix.com> wrote in message news:<roy-CA3285.20323003022002 at news1.panix.com>...
> 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.

Roy,
PyDoc.py located in the Lib subdirectory does just what your asking
using inspect.py as mentioned in another reply to your question.  By
using PyDoc.py as an example you should be able to extract what you
need and modify to your specs.  Giving appropriate credit, of course.

Regards,
Barry



More information about the Python-list mailing list