While using data descriptors in building classes, I came across a strange behavior of getattr function on a class.
# 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
LHS return an empty string, while the RHS returns an instance of String
. I thought getattr
on an object was to get the value for the key inside the __dict__
. How does getattr
function work on a class object?