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


Groups > comp.lang.python > #112134

Learning Descriptors

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Gerald Britton <gerald.britton@gmail.com>
Newsgroups comp.lang.python
Subject Learning Descriptors
Date Sun, 31 Jul 2016 08:33:18 -0400
Lines 42
Message-ID <mailman.73.1469968402.6033.python-list@python.org> (permalink)
References <CAPxRSnZ0e-4gJJnM_Hg2HW3pkqgPebJrz=BYn4eST3+vX1Txkg@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-Trace news.uni-berlin.de WoZNE3qXzSY7eVXhsIYmbA/ajb1n/qnvgapOAc6vbxxg==
Return-Path <gerald.britton@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'classes,': 0.05; 'descriptor': 0.09; 'key)': 0.09; 'obj,': 0.09; 'def': 0.13; '&gt;&gt;&gt;': 0.15; '42.': 0.16; 'key):': 0.16; 'read:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'self)': 0.16; 'subject:Learning': 0.16; 'case.': 0.18; 'copied': 0.18; '>>>': 0.20; 'function,': 0.22; 'this:': 0.23; "doesn't": 0.26; 'skip:_ 20': 0.26; 'message-id:@mail.gmail.com': 0.27; 'this.': 0.28; 'looks': 0.29; "i'm": 0.30; 'skip:_ 10': 0.32; 'class': 0.33; 'url:python': 0.33; 'usually': 0.33; "skip:' 20": 0.34; 'received:google.com': 0.35; 'so,': 0.35; 'something': 0.35; 'expected': 0.35; 'but': 0.36; 'url:org': 0.36; 'to:addr:python- list': 0.36; 'really': 0.37; 'skip:& 10': 0.37; 'missing': 0.37; 'skip:o 20': 0.38; 'why': 0.39; 'test': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.40; 'url:3': 0.60; 'today,': 0.62; 'here': 0.66; 'answer:': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to; bh=dBCNh+BfqTlSJ+EiHnTLdf9Z2LJE3TPIqMHz0n1OaLs=; b=al0wqhI/299QvIuxUi01JIk+VMNwJgpnRoeOdiHnO2rCXTNZXmzHsKbvJt3G5Nz1WJ 5+cy1u+K4rDKEbZqR4aePOTXEGTa6kf7eHUuqwqhVGXOvCSAyomtwD18c8sKI4BXhPNJ jUrhe2qn1V2AWRGGaTQJjdsaY9LqZGTMoBBbpGBh53BMjGgEz9PnSJwa7KsZNtlvvvwS srPrFDcRgBQz0vsq9JwniyHBcsGhyrqRKWEW2EwgkzmFWfD9XEhdHKNu5zEZNWemXsio OQGORnR4WpS1TyIjTh02foyY7hfUdUMQ/KlyS97JqfCPtEc9iVkxWUT7sZBThCGA7aam WiHg==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=dBCNh+BfqTlSJ+EiHnTLdf9Z2LJE3TPIqMHz0n1OaLs=; b=YGYybAXf3sT4JPACBsmDg4W1U9/KUEtMg8mQe1kTcRwjtmyikz+OHU9ZPohvdh3lA3 ep7kDN0yb5YkFyIHb7wC5aBN70pe29jtUAdybFHrEYvORBCU7klU0odEvvaPZVMWg2Cl P77/sinZiCBVCk44LjNrMUh9DF9G9i0+L0iqJiInszq7dUaGjiRXYOScJk6T9sZUt3jy jY427cnNEId3vDSAbODh/n2TaHNybJFI0mR3udtunph68HjiSii+lTvT9wlVN+IamdhS DZ2pvTZLtI/Mzuv1KtruX6kJ4nD9k1XHfoLGMLR5dRuiKVATQUCe6M9L4hHPIn2ZdM8f cNZg==
X-Gm-Message-State AEkoouvIbMji6JVk2vpDZqMA4Trb6sIGXkKV9eGcRRo4qCedriyJz0adxBTzEU7G59g3ZcZSymUhUJT8ubHdSg==
X-Received by 10.107.47.152 with SMTP id v24mr51826536iov.154.1469968398939; Sun, 31 Jul 2016 05:33:18 -0700 (PDT)
X-Content-Filtered-By Mailman/MimeDel 2.1.22
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <CAPxRSnZ0e-4gJJnM_Hg2HW3pkqgPebJrz=BYn4eST3+vX1Txkg@mail.gmail.com>
Xref csiph.com comp.lang.python:112134

Show key headers only | View raw


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?

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web