FEEDBACK WANTED: Type/class unification

Paul Prescod paulp at
Sun Jul 29 18:39:52 CEST 2001

Guido van Rossum wrote:
> Paul Prescod <paulp at> writes:
> > I'm reading the section on the introspection API but I don't understand
> > something.
> >
> > 1. If I am sitting at an interpreter prompt and I have an object how do
> > I figure out the complete list of currently available attributes
> > available on that object.
> Same as currently if it's a class: look in inst.__dict__ for instance
> vars, then look in inst.__class__ for methods and class vars, then
> recursively look through int.__class__.__bases__ for inherited methods
> and class vars.  Weed out duplicates.

Is there any reason that this is not defined as a built-in function? I
propose a function called attrs().

> > 2. If I have an object that hides its attributes behind an __getattr__,
> > is there any way to tweak the list returned by the API described above?

You didn't answer this one. If I'm wrapping a COM object and I want to
dynamically provide the list of methods and other attributes when some
asks (i.e. "override" dir() or attrs()).

> ....  I'd like something as close to
> > this as is realistically possible:
> >
> > def attrs(x):
> >       return [y for y in all_possible_strings if hasattr(x, y)]
> (Note: this may return an infinite list!)

Right, that's why I'm asking for something as *close as realistically
possible*. i.e. a built-in function that gets me the attributes list.

99% of the time when I use dir(), I wish it behaved as I described
attrs() and I just have to work around its limitations. I would like for
there to be a function that returns as many attributes as is
realistically possible.

> BTW, now also linked from the home page side bar.

Great! Thanks!

Take a recipe. Leave a recipe.  
Python Cookbook!

More information about the Python-list mailing list