Getting a list of an object's methods?

Michele Simionato mis6 at pitt.edu
Mon Jun 23 08:47:18 EDT 2003


Alexander Schmolck <a.schmolck at gmx.net> wrote in message news:<yfsel1l651p.fsf at black132.ex.ac.uk>...
> > For example, 'mro' isn't in int's dir(), although it is (the name of) a
> > method of type and in type's __dict__.
> 
> Indeed, so this looks like a bug to that should be filed (unless `mro` somehow
> doesn't qualify as a 'member' -- I can't see why, but this is something I
> leave to the wizards).
> 
> If these omissions turn out to be important of the OP's desired application,
> then he'll indeed need to consider a further alternative (maybe based upon the
> union of `inspect.getmembers` and the `__dict__.keys()`, where the latter are
> available).
> 
> Anyway, this example highlights a problem I'm getting increasingly concerned
> about, namely the erosion of python's metaprogramming capabilities. I don't
> think "What methods does this object have?" is a question that only a python
> wizard should be able to ask and receive a well-defined answer.
> 
> 'as

The problem with .mro() is that it is a method of the metaclass
'type':

>>> dir(type)
['__base__', '__bases__', '__basicsize__', '__call__', '__class__',
'__cmp__', '__delattr__', '__dict__', '__dictoffset__', '__doc__',
'__flags__', '__getattribute__', '__hash__', '__init__',
'__itemsize__', '__module__', '__mro__', '__name__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
'__subclasses__', '__weakrefoffset__', 'mro']

IIRC, Tim Peters wrote in this newsgroup some months ago that 'dir'
was
designed in such a way to not report metaclass methods. The idea was
that the typical user doesn't want too much information. Therefore
'dir'
is not meant to be exaustive:

>>> help(dir)
Help on built-in function dir:

dir(...)
    dir([object]) -> list of strings
    
    Return an alphabetized list of names comprising (some of) the
attributes
                                                     ^^^^^^^^
    of the given object, and of attributes reachable from it:

Nevertheless, one may disagree with this attitude, and I think that if
enough users protest enough, we may get a more powerful 'dir' in the
future.

                                         Michele




More information about the Python-list mailing list