title: Confusions about Python Descriptors and <Descriptor HowTo Guide>
body: <p>Recently I read the <a href="http://docs.python.org/2/howto/descriptor.html" rel="nofollow noreferrer">official HOW-TO about Python descriptors</a>, which actually derives from <a href="http://users.rcn.com/python/download/Descriptor.htm" rel="nofollow noreferrer">an essay</a> written by Raymond Hettinger long time ago. But after reading it for several times, I am still confused about some parts of it. I will quote some paragraphs, followed by my confusions and questions.</p>
  <hr />
  <blockquote>
  <p>If an instance’s dictionary has an entry with the same name as a data descriptor, the data descriptor takes precedence. If an instance’s dictionary has an entry with the same name as a non-data descriptor, the dictionary entry takes precedence.</p>
  </blockquote>
  <ul>
  <li>Is this the same with class? What is the precedence chain of a class if its dictionary has an entry with the same name as a data/non-data descriptor?</li>
  </ul>
  <hr />
  <blockquote>
  <p>For objects, the machinery is in <code>object.__getattribute__()</code> which transforms <code>b.x</code> into <code>type(b).__dict__['x'].__get__(b, type(b))</code>. The implementation works through a precedence chain that gives data descriptors priority over instance variables, instance variables priority over non-data descriptors, and assigns lowest priority to <code>__getattr__()</code> if provided.</p>
  <p>For classes, the machinery is in <code>type.__getattribute__()</code> which transforms <code>B.x</code> into <code>B.__dict__['x'].__get__(None, B)</code>.</p>
  </blockquote>
  <ul>
  <li>The above two paragraphs tell the process of a descriptor's being invoked automatically upon attribute access. It lists the difference between attribute's being accessed by an instance (<code>b.x</code>) and a class (<code>B.x</code>). However, here are my confusions:
  <ul>
  <li>if the attribute of a class or an instance is not a descriptor, will the transformation (i.e., transforms <code>b.x</code> into <code>type(b).__dict__['x'].__get__(b, type(b))</code> and <code>B.x</code> into <code>B.__dict__['x'].__get__(None, B)</code>) still proceed? Is returning the attribute in this class's or instance's <strong>dict</strong> directly simpler?</li>
  <li>If an instance's dictionary has an entry with the same name as a non-data descriptor, according to the precedence rule in the first quote, the dictionary entry takes precedence, at this time will the transformation still proceed? Or just return the value in its <strong>dict</strong>?</li>
  </ul>
  </li>
  </ul>
  <hr />
  <blockquote>
  <p>Non-data descriptors provide a simple mechanism for variations on the usual patterns of binding functions into methods.</p>
  </blockquote>
  <ul>
  <li>Is non-data descriptors chosen because functions/methods can only be <strong>gotten</strong>, but cannot be <strong>set</strong>?</li>
  <li>What's the underlying mechanism for binding functions into methods? Since class dictionaries store methods as functions, if we call the same method using a class and its instance respectively, how can the underlying function tell whether its first argument should be <strong>self</strong> or not?</li>
  </ul>
  <hr />
  <blockquote>
  <p>Functions have a <code>__get__()</code> method so that they can be converted to a method when accessed as attributes. The non-data descriptor transforms a <code>obj.f(*args)</code> call into <code>f(obj, *args)</code>. Calling <code>klass.f(*args)</code> becomes f(*args).</p>
  </blockquote>
  <ul>
  <li>How can a non-data descriptor transform a <code>obj.f(*args)</code> call into <code>f(obj, *args)</code>?</li>
  <li>How can a non-data descriptor transform a <code>klass.f(*args)</code> call into <code>f(*args)</code>?</li>
  <li>What's the underlying mechanism of the above two transformations? Why do differences exist between class and instance?</li>
  <li>What does the role <code>__get__()</code> method play under the above circumstance?</li>
  </ul>