title: Avoiding infinite loops in __getattribute__
body: <p>The method <code>__getattribute__</code> needs to be written carefully in order to avoid the infinite loop. For example:</p>
  <pre><code>class A:
      def __init__(self):
          self.x = 100
  
      def __getattribute__(self, x):
          return self.x
  
  &gt;&gt;&gt; a = A()
  &gt;&gt;&gt; a.x    # infinite looop
  RuntimeError: maximum recursion depth exceeded while calling a Python object
  
  
  class B:
      def __init__(self):
          self.x = 100
  
      def __getattribute__(self, x):
          return self.__dict__[x]
  
  &gt;&gt;&gt; b = B()
  &gt;&gt;&gt; b.x    # infinite looop
  RuntimeError: maximum recursion depth exceeded while calling a Python object
  </code></pre>
  <p>Hence we need to write the method in this way:</p>
  <pre><code>class C:
      def __init__(self):
          self.x = 100
  
      def __getattribute__(self, x):
          # 1. error
          # AttributeError: type object 'object' has no attribute '__getattr__'
          # return object.__getattr__(self, x)
          
          # 2. works
          return object.__getattribute__(self, x)
          
          # 3. works too
          # return super().__getattribute__(x)
  </code></pre>
  <p>My question is why does <code>object.__getattribute__</code> method work? From where does <code>object</code> get the <code>__getattribute__</code> method? And if <code>object</code> does not have any <code>__getattribute__</code>, then we are just calling the same method on class <code>C</code> but via the super class. Why, then calling the method via super class does not result in an infinite loop?</p>