__getattribute__ 容易触发无限递归,因为其内部访问任何属性(如self.__dict__)都会再次调用自身;正确做法是所有属性读取必须显式调用object.__getattribute__(self, name)或super().__getattribute__(name)。

为什么 __getattribute__ 容易触发无限递归
因为每次访问实例的任何属性(包括 self.__dict__、self._cache 这类内部变量),都会再次调用 __getattribute__。如果你在方法里直接写 self.x 或 self.__dict__,就等于在递归入口里又踩了一脚递归入口。
正确防护:一律走 object.__getattribute__ 绕过自定义逻辑
所有需要读取属性值的地方,必须显式调用父类实现,不能依赖点号访问。这是唯一可靠方式。
- 想读原始
__dict__?用object.__getattribute__(self, '__dict__') - 想查某个字段是否已存在?用
object.__getattribute__(self, 'field_name'),别用hasattr(self, 'field_name')(它内部会触发__getattribute__) - 想 fallback 到默认行为?统一用
super().__getattribute__(name),它等价于object.__getattribute__(self, name) - 如果要访问的是描述符(比如
@property),也必须走super(),否则可能跳过其__get__逻辑
常见错误写法与对应修复
下面这些写法看着自然,但全都会崩:
# ❌ 错误:self.__dict__ 触发递归
def __getattribute__(self, name):
if name in self.__dict__: # boom
return self.__dict__[name]
✅ 正确:绕过自定义逻辑取 dict
def getattribute(self, name):
d = object.getattribute(self, 'dict')
if name in d:
return d[name]
再比如缓存场景:
# ❌ 错误:self._cache 是普通属性,访问即递归
def __getattribute__(self, name):
cache = self._cache # boom
✅ 正确:用 super() 或 object.getattribute
def getattribute(self, name):
try:
cache = super().getattribute('_cache')
except AttributeError:
cache = {}
object.setattr(self, '_cache', cache)
要不要用 __getattr__ 替代?
如果只是想拦截「不存在的属性」,__getattr__ 更安全、更简单,它只在 __getattribute__ 抛出 AttributeError 后才被调用,天然不递归。
- 适合做 fallback、动态生成、日志兜底
- 不适合做全局属性拦截(比如统一打日志、权限校验、类型转换),因为它根本看不到已存在的属性
- 很多场景其实不需要
__getattribute__—— 先问自己:真要拦住self.existing_field吗?还是只拦self.missing_thing?
真正需要 __getattribute__ 的地方不多,一旦用,就必须对每个属性访问都保持警惕,连 self.__class__ 和 self.__module__ 都得走 super()。










