Consider the following difference between classic classes and the new style classes.
class A():
data = 'abcd'
def __getattr__(self, name):
return getattr(self.data, name)
class B(object):
data = 'abcd'
def __getattr__(self, name):
return getattr(self.data, name)
print(A()[0]) # Prints 'a'
print(B()[0]) # TypeError: 'B' object does not support indexing
I do know that explanation for this property is that new style objects attribute search starts at class instead of instances for builtin operations. But the class object too has __getattr__ defined and why it doesn't get invoked for the missing attribute here which is __getitem__.
__hash__()
and__repr__()
that are implemented by all objects, including type objects. If the implicit lookup of these methods used the conventional lookup process, they would fail when invoked on the type object itself – Jon Clements