descriptors and old-style classes

A

Adrien Di Mascio

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

Cheers,
Adrien.
 
J

John Roth

Adrien Di Mascio said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top