Access the methods of a class

Gary Herron gherron at islandtraining.com
Fri Oct 31 11:42:01 EST 2003


On Friday 31 October 2003 08:10 am, Fernando Rodriguez wrote:
> Hi,
>
> I have a base class with a 'sanity-check' method. This method should
> iterate through all the methods and check if they are all 'thunks' (zero
> parameter functions, well actually, 1 parameter: self).
>
> How can I access the list of methods of a given object?  BTW, this class
> will be inherited from, so it should work with hte derived classes too.
>
> How can I check the number of parameters of a given function object?
>
> TIA O:-)
>
> PS Any pointers to a python reflection tutorial, would also be appreciated.

The inspect module should provide everything you need here.

>From the manual:


  inspect -- Inspect live objects 

    New in version 2.1. 


    The inspect module provides several useful functions to help get
    information about live objects such as modules, classes, methods,
    functions, tracebacks, frame objects, and code objects. For
    example, it can help you examine the contents of a class, retrieve
    the source code of a method, extract and format the argument list
    for a function, or get all the information you need to display a
    detailed traceback.


Gary Herron







More information about the Python-list mailing list