FEEDBACK WANTED: Type/class unification

Guido van Rossum guido at zope.com
Sun Jul 29 13:08:03 EDT 2001


> > 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().

Yes, it's a lot less useful than you seem to think.  If you are
interested in documentation, it's better to use something like pydoc
which gives much more details.

Right now, pydoc doesn't understand the new types yet, but eventually,
before 2.2 final, pydoc (or more likely inspect.py) will be able to
introspect according to PEP 252.

> >...
> > > 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 thought I answered it by saying "you can't do that".

But now that you've given more information, I understand your question
better.  I think you should be able to override the __dict__ attr of
the type and put pseudo descriptors in there (see PEP 252 for the
descriptor API).

> > ....  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.

Hm, I'd like you to tell me more about how you use dir().  I find
getting the entire list of attributes overwhelming.

--Guido van Rossum (home page: http://www.python.org/~guido/)




More information about the Python-list mailing list