[Python-Dev] Attribute lookup (was Re: python-dev Summary for 2003-05-01 through 2003-05-15)

Aahz aahz@pythoncraft.com
Sun, 18 May 2003 21:58:22 -0400


[Normally I send my corrections to Brett privately, but since I'm taking
a whack at attribute lookup, I figured this ought to be public.]

On Sun, May 18, 2003, Brett C. wrote:
>
> The only thing I would like help with this summary is if someone knows 
> the attribute lookup order (instance, class, class descriptor, ...) off 
> the top of their heads, can you let me know?  If not I can find it out 
> by going through the docs but I figure someone out there has to know it 
> by heart and any possible quirks (like whether descriptors take 
> precedence over non-descriptor attributes).

This gets real tricky.  For simple attributes of an instance, the order
is instance, class/type, and base classes of the class/type (but *not*
the metaclass).  However, method resolution of the special methods goes
straight to the class.  Finally, if an attribute is found on the
instance, a search goes through the hierarchy to see whether a set
descriptor overrides (note specifically that it's a set descriptor;
methods are implemented using get descriptors).

I *think* I have this right, but I'm sure someone will correct me if I'm
wrong.

>     LookupError exists and subclasses both IndexError and KeyError. 
> Rather handy when you don't care whether you are dealing with a list or 
> dictionary but do care if what you are looking for doesn't exist.
>     os.walk also gained a parameter argument called onerror that takes 
> a function that will be passed any exception raised by os.walk as it 
> does its thing; previously os.walk ignored all errors.

"and has as subclasses"
-- 
Aahz (aahz@pythoncraft.com)           <*>         http://www.pythoncraft.com/

"In many ways, it's a dull language, borrowing solid old concepts from
many other languages & styles:  boring syntax, unsurprising semantics,
few automatic coercions, etc etc.  But that's one of the things I like
about it."  --Tim Peters on Python, 16 Sep 93