Class attributes, instances and metaclass __getattribute__

Pedro Werneck pedro.werneck at terra.com.br
Tue Aug 8 12:35:29 EDT 2006


On 8 Aug 2006 07:24:54 -0700
"Ziga Seilnacht" <ziga.seilnacht at gmail.com> wrote:

> [snip]
> > Well... I'm not talking about metaclass attributes... that's
> > perfectly consistent, agreed.
> >
> > I'm saying that when the class implements a custom __getattribute__,
> > when you try to access the instance attributes from itself, it uses
> > it. But if the class is a metaclass, instances of its instances have
> > acess to the attribute anyway, but don't use the custom
> > __getattribute__ you implemented.
> 
> Attribute lookup for instances of a class never calls metaclass'
> __getattribute__() method. This method is called only when you
> access attributes directly on the class.

Well... thanks for the answer. 

As I said on the first mail, I noticed this when I was explaining
descriptors and methods to someone else... I implemented a pure-python
Method class to show him exactly how it works. 

But, since their __get__ call is available at the class too, my first
thought was that it was implemented at the metaclass __getattribute__,
and when an instance tries to get a class attribute it would fail on its
own __getattribute__, use the bound method at its class and make the
call. 

After implementing a test metaclass I noticed it doesn't work this way,
and even if it's a bit inconsistent (especially in the case of 'virtual'
attributes), it seemed to me the reasonable thing to do, exactly for
what you mentioned on your code... someone could easily break a lot of
stuff doing it the wrong way, instead if not using __dict__.

I mailed the list because someone else thought it might be a bug and I
was in doubt... now it's clear it was the right thing to do.


Regards,

-- 
Pedro Werneck



More information about the Python-list mailing list