descriptors and old-style classes

John Roth newsgroups at jhrothjr.com
Tue Jan 27 15:23:44 EST 2004


"Adrien Di Mascio" <adim at logilab.fr> wrote in message
news:slrnc1d88e.qr.adim at lacerta.logilab.fr...
> Hi,
>
> I've recently discovered that descriptors can be used with old-style
> classes if they don't define a __set__ method (and this would be why
> class and static methods are usable with old-style classes).
> I understand why the __set__ method is not called with old-style
> classes, but I don't understand why __get__ *is* called.
> I thought the transformation from :
>   a.x
> into :
>   type(a).__dict__['x'].__get__(a, type(a))
> was done in object.__getattribute__ (or type.__getattribute__), but then
> this does not explain why this also seems to work with old-style
> classes.
>
> Could someboby help me here ??

At a rough guess, the search logic is in __getattribute__, but the
actual invocation is probably in the bytecode somewhere.
__getattribute__, after all, is supposed to return the attribute
requested, not to invoke it.

So on an old style class, the standard search will find the
descriptor object just fine, then the bytecode will detect it
has a __get__ method and invoke it.

John Roth
>
> Cheers,
> Adrien.
>
> -- 
> Adrien Di Mascio
> LOGILAB, Paris (France).
> http://www.logilab.com   http://www.logilab.fr   http://www.logilab.org
>





More information about the Python-list mailing list