Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #112134 > unrolled thread

Learning Descriptors

Started byGerald Britton <gerald.britton@gmail.com>
First post2016-07-31 08:33 -0400
Last post2016-07-31 08:33 -0400
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Learning Descriptors Gerald Britton <gerald.britton@gmail.com> - 2016-07-31 08:33 -0400

#112134 — Learning Descriptors

FromGerald Britton <gerald.britton@gmail.com>
Date2016-07-31 08:33 -0400
SubjectLearning Descriptors
Message-ID<mailman.73.1469968402.6033.python-list@python.org>
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?

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web