为什么 str() 不用 __getattribute__ 得到 __str__ 以及如何产生效果?

Why doesn't str() use __getattribute__ to get __str__ and how to produce the effect?

当我在具有重载 __getattribute__ 方法的对象上调用 str() 时,它似乎没有使用它,而是直接调用 __str__。是否有其他一些我应该修改的功能或让它使用 __getattribute__ 的方法?如果我直接重载 __str__ 它会按预期运行,但这并不适合我的需要。

class A(object):
    def __getattribute__(self, attr):
        if attr == "__str__":
            return lambda: "Hello"
        return object.__getattribute__(self, attr)

x = A()

print(x)
print(str(x))
print(x.__str__())

输出:
<main.0x000001FDF7AEA760 处的对象>
<main.0x000001FDF7AEA760 处的对象>
你好

预期输出:
你好
你好
你好

是的,这是 documented behavior:

This method may still be bypassed when looking up special methods as the result of implicit invocation via language syntax or built-in functions.

here

For custom classes, implicit invocations of special methods are only guaranteed to work correctly if defined on an object’s type, not in the object’s instance dictionary

...

In addition to bypassing any instance attributes in the interest of correctness, implicit special method lookup generally also bypasses the __getattribute__() method even of the object’s metaclass...

...

至于为什么:

Bypassing the __getattribute__() machinery in this fashion provides significant scope for speed optimisations within the interpreter, at the cost of some flexibility in the handling of special methods (the special method must be set on the class object itself in order to be consistently invoked by the interpreter).