Use of getattr on a class with data descriptor
<p>While using data descriptors in building classes, I came across a strange behavior of getattr function on a class.</p>

<pre><code># this is a data descriptor
class String(object):
    def __get__(self, instance, owner):
        pass
    def __set__(self, instance, value):
        pass

# This defines a class A with 'dot' notation support for attribute 'a'
class A(object):
    a = String()

obj = A()
assert getattr(A, 'a') is A.__dict__['a']
# This raises AssertionError
</code></pre>

<p>LHS return an empty string, while the RHS returns an instance of <code>String</code>. I thought <code>getattr</code> on an object was to get the value for the key inside the <code>__dict__</code>. How does <code>getattr</code> function work on a class object?</p>

<p><code>getattr(A, 'a')</code> is the same as <code>A.a</code>. This calls the respective descriptor, if present. So it provides the value presented by the descriptor, which is <code>None</code>. </p>

python python-2.7 class getattr