Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Learning Descriptors Date: Sun, 31 Jul 2016 07:03:39 -0600 Lines: 60 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de 6OWcIy0GFqxyD2ecLR+wnQBH3WBCRIfgDZh51fH9A3/g== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'classes,': 0.05; 'defines': 0.07; 'cc:addr:python-list': 0.09; 'descriptor': 0.09; 'key)': 0.09; 'obj,': 0.09; 'itself.': 0.11; 'def': 0.13; '2016': 0.16; '42.': 0.16; 'key):': 0.16; 'read:': 0.16; 'received:io': 0.16; 'received:mail-oi0-x22a.google.com': 0.16; 'received:psf.io': 0.16; 'self)': 0.16; 'subject:Learning': 0.16; 'wrote:': 0.16; 'obviously': 0.16; 'case.': 0.18; 'copied': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '31,': 0.22; 'function,': 0.22; 'am,': 0.23; 'this:': 0.23; 'header:In-Reply- To:1': 0.24; "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; 'received:google.com': 0.35; 'so,': 0.35; 'instance': 0.35; 'something': 0.35; 'expected': 0.35; 'but': 0.36; 'url:org': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'missing': 0.37; 'skip:o 20': 0.38; 'why': 0.39; 'test': 0.39; 'sure': 0.39; 'some': 0.40; 'url:3': 0.60; 'real': 0.62; 'today,': 0.62; 'here': 0.66; 'jul': 0.72; 'answer:': 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:cc :content-transfer-encoding; bh=P3y1WrahFt+OL+JqJn5cx1dRPmt08c5OgLopRmTO1eA=; b=SLMdC4k+aw2uM2awQXwMYAQJp9X0gBrr5+Ln85LoOeuJ61Y7Z7/ULaigTuThGDzLLF tR0as7qOie19rhodoKhcQAC6hoKA+BJDWcEJEIFV5a7+dwyPFpibSK4U7IbXQDxTxuai 9+e4nFEdFUFzesyKuD6vUnwhwLVAwkh6yTvMqRdfHTNCVgiG5UZ8jVJtLXYGfWeJ8+oQ DQjStcIds2wg7htm/1YAA8lEUFT8v6QiWFnfItr345Df190fwKBPEA9lCOaggtmf/RuK WmcMJnrT+cLlFyQN6fUT8p+CvJFxIBXFqUH/EkDjVidcMxNCQqWSJARRFp7vL9Bk1D/V ZXaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:cc:content-transfer-encoding; bh=P3y1WrahFt+OL+JqJn5cx1dRPmt08c5OgLopRmTO1eA=; b=T05IPaWtyPynw+mRlJ6XavubW/hvqj53S2E9b7MhKk+QzsCxEYqppMaSNs9ynieZ1I veFemsKIhNzhF9gQC8iUMDoi2Te1iUwlqTXrotN7XcjcFcliMm0yJaZz7P2H5h131Z9h tVL3yF1iVWhqGmL9rHyD7VOIk8znwmzyGA+sGRidwFr9rmA01+mMGGVi9xuj1S3/ZDMX C6LChV8Zj+Ded2bE+llX8aLnM1RIRXmJDQe9tGCaDUl8Ot6z/EbxxD73tCvi+dBuxo0v on2ytNC9vJQ84gXP8BZKhSQ8rcwMhokPmbNzMZ3U+neTbxWm7e7iB7laNYG2ZiA++FZn 9zfg== X-Gm-Message-State: AEkooutC5QYdh1m633jRRjK+kCsWqxm6E0vEpzehna9US5MLaIqSvgjfUdyOmGldKix2IJsMob1pbkZ8YJm2uQ== X-Received: by 10.202.88.215 with SMTP id m206mr10760816oib.47.1469970259533; Sun, 31 Jul 2016 06:04:19 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:112137 On Sun, Jul 31, 2016 at 6:33 AM, Gerald Britton wrote: > Today, I was reading RH's Descriptor HowTo Guide at > > https://docs.python.org/3/howto/descriptor.html?highlight=3Ddescriptors > > 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=3DNone): > return 42 > >>>> 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 =3D 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 <:=E2= =80=91|) , > 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: py> class Answer: ... def __get__(self, obj, type): ... return 42 ... py> class The: ... Answer =3D Answer() ... py> The.Answer 42