Learning Descriptors

Gerald Britton gerald.britton at gmail.com
Sun Jul 31 13:11:08 EDT 2016


>On Sun, Jul 31, 2016 at 6:33 AM, Gerald Britton
><gerald.britton at gmail.com> wrote:
>> Today, I was reading RH's Descriptor HowTo Guide at
>>
>> https://docs.python.org/3/howto/descriptor.html?highlight=descriptors
>>
>> I just really want to fully "get" this.
>>
>> So I put together a little test from scratch.  Looks like this:
>>
>> class The:
>>     class Answer:
>>         def __get__(self, obj, type=None):
>>             return 42
>>
>>>>> The.Answer
>> <class '__main__.The.Answer'>
>>>>>
>>
>> but, I expected to see 42.
>>
>> So, digging deeper I read:
>>
>> For classes, the machinery is in type.__getattribute__() which transforms
>> B.x into B.__dict__['x'].__get__(None, B). In pure Python, it looks like:
>>
>> def __getattribute__(self, key):
>>     "Emulate type_getattro() in Objects/typeobject.c"
>>     v = object.__getattribute__(self, key)
>>     if hasattr(v, '__get__'):
>>         return v.__get__(None, self)
>>     return v
>>
>> OK, so I copied this function, then ran it and got:
>>
>>>>> __getattribute__(The, 'Answer')
>> 42
>>
>> So, what I don't get is why the "B.x into B.__dict__['x'].__get__(None,
B)"
>> part doesn't work in my case.
>>
>> I'm sure I'm missing something here (that`s usually the case for me
<:‑|) ,
>> but what?
>
>Obviously that __getattribute__ is not exactly like the real one. The
>real one looks up __get__ as a *method* of B.__dict__['x'], which
>requires that B.__dict__['x'] be an instance of some class that
>defines __get__, not the class itself. Try this:

Thanks Ian, that helps, though I suppose I should point out that the
article covers both objects and classes.  It was the section about classes
I was referring to.  Reading it again, it's obvious that 'B' is a class and
'x' is some attribute of B.  What's not clear is that 'x' must be an
*instance* of something for this to work as I expected.

Even so, If I translate my The.Answer to
The.__dict__['Answer'].__get__(None, The), (as per the article) it works as
I expect:

>>> The.__dict__['Answer'].__get__(None, The)
42

Note that 'Answer' is not an instance; it's another class.  So I suppose
the document is a little ambiguous on that point.

That's OK. I just need a mental note to remember what it really means.

>
>py> class Answer:
>...     def __get__(self, obj, type):
>...         return 42
>...
>py> class The:
>...     Answer = Answer()
>...
>py> The.Answer
>42

On Sun, Jul 31, 2016 at 8:33 AM, Gerald Britton <gerald.britton at gmail.com>
wrote:

> Today, I was reading RH's Descriptor HowTo Guide at
>
> https://docs.python.org/3/howto/descriptor.html?highlight=descriptors
>
> I just really want to fully "get" this.
>
> So I put together a little test from scratch.  Looks like this:
>
> class The:
>     class Answer:
>         def __get__(self, obj, type=None):
>             return 42
>
> >>> The.Answer
> <class '__main__.The.Answer'>
> >>>
>
> but, I expected to see 42.
>
> So, digging deeper I read:
>
> For classes, the machinery is in type.__getattribute__() which transforms
> B.x into B.__dict__['x'].__get__(None, B). In pure Python, it looks like:
>
> def __getattribute__(self, key):
>     "Emulate type_getattro() in Objects/typeobject.c"
>     v = object.__getattribute__(self, key)
>     if hasattr(v, '__get__'):
>         return v.__get__(None, self)
>     return v
>
> OK, so I copied this function, then ran it and got:
>
> >>> __getattribute__(The, 'Answer')
> 42
>
> So, what I don't get is why the "B.x into B.__dict__['x'].__get__(None,
> B)" part doesn't work in my case.
>
> I'm sure I'm missing something here (that`s usually the case for me <:‑|) ,
> but what?
>
>


-- 
Gerald Britton, MCSE-DP, MVP
LinkedIn Profile: http://ca.linkedin.com/in/geraldbritton



More information about the Python-list mailing list