Python: unikanie nieskończonych pętli w __getattribute__

Metoda__getattribute__ musi być napisane starannie, aby uniknąć nieskończonej pętli. Na przykład:

class A:
    def __init__(self):
        self.x = 100

    def __getattribute__(self, x):
        return self.x

>>> a = A()
>>> 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]

>>> b = B()
>>> b.x    # infinite looop
RuntimeError: maximum recursion depth exceeded while calling a Python object

Dlatego musimy napisać metodę w ten sposób:

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)

Moje pytanie brzmi: dlaczegoobject.__getattribute__ metoda działa? Skądobject dostaje__getattribute__ metoda? I jeśliobject nie ma żadnych__getattribute__, wtedy po prostu wywołujemy tę samą metodę na klasieC ale za pośrednictwem super klasy. Dlaczego więc wywołanie metody za pośrednictwem super klasy nie powoduje nieskończonej pętli?

questionAnswers(3)

yourAnswerToTheQuestion