How to display all the methods supported by an instance using dir( )..please hel

Skip Montanaro skip at pobox.com
Fri Jun 22 07:55:00 EDT 2001


    Karthik> t = test()
    Karthik> dir(t)

    Karthik> But it does'nt list func :-(. 

Dir is pretty straightforward.  For objects that have a __dict__ attribute,
it just lists the dictionary's keys.  A class instance's methods are stored
in the class's __dict__, so you could view them with

    dir(t.__class__)

That will only get the methods defined in t.__class__, however.  If it has
base classes, you'd be better off using the inspect module:

    >>> class foo:
    ...   def meth1(self, x):
    ...     pass
    ... 
    >>> class bar(foo):
    ...   def meth2(self, y):
    ...     pass
    ... 
    >>> t = bar()
    >>> dir(t.__class__)
    ['__doc__', '__module__', 'meth2']
    >>> import inspect
    >>> inspect.getmembers (t.__class__)
    [('__doc__', None), ('__module__', '__main__'), ('meth2', <unbound method bar.meth2>)]

Good help for inspect is available online via pydoc:

    >>> import pydoc
    >>> pydoc.help(inspect)
    ...

Cheers,

-- 
Skip Montanaro (skip at pobox.com)
(847)971-7098




More information about the Python-list mailing list