What is the difference between type.__getattribute__ and object.__getattribute__?

Posted by Neil G on Stack Overflow See other posts from Stack Overflow or by Neil G
Published on 2014-06-10T21:10:46Z Indexed on 2014/06/10 21:24 UTC
Read the original article Hit count: 199

Given:

In [37]: class A:
   ....:     f = 1
   ....:

In [38]: class B(A):
   ....:     pass
   ....:

In [39]: getattr(B, 'f')
Out[39]: 1

Okay, that either calls super or crawls the mro?

In [40]: getattr(A, 'f')
Out[40]: 1

This is expected.

In [41]: object.__getattribute__(A, 'f')
Out[41]: 1

In [42]: object.__getattribute__(B, 'f')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-42-de76df798d1d> in <module>()
----> 1 object.__getattribute__(B, 'f')

AttributeError: 'type' object has no attribute 'f'

What is getattribute not doing that getattr does?

In [43]: type.__getattribute__(B, 'f')
Out[43]: 1

What?! type.__getattribute__ calls super but object's version doesn't?

In [44]: type.__getattribute__(A, 'f')
Out[44]: 1

© Stack Overflow or respective owner

Related posts about python

Related posts about getattr