首页 > 解决方案 > 使用 __setattr__ 和 __getattr__ 与 __slots__ 进行委托而不触发无限递归

问题描述

class A:
    __slots__ = ("a",)
    def __init__(self) -> None:
        self.a = 1

class B1:
    __slots__ = ("b",)
    def __init__(self, b) -> None:
        self.b = b

    def __getattr__(self, k):
        return getattr(self.b, k)

    def __setattr__(self, k, v):
        setattr(self.b, k, v)

class B2:
    __slots__ = ("b",)
    def __init__(self, b) -> None:
        self.b = b

    def __getattr__(self, k):
        return getattr(super().__getattr__("b"), k)

    def __setattr__(self, k, v):
        setattr(super().__getattr__("b"), k, v)

class B3:
    __slots__ = ("b",)
    def __init__(self, b) -> None:
        self.b = b

    def __getattr__(self, k):
        return getattr(getattr(super(), "b"), k)

    def __setattr__(self, k, v):
        setattr(getattr(super(), "b"), k, v)

a = A()
b = B1(a)
print(b.a) # RecursionError: maximum recursion depth exceeded

b = B2(a)
print(b.a) # AttributeError: 'super' object has no attribute '__getattr__'

b = B3(a)
print(b.a) # AttributeError: 'super' object has no attribute 'b'

标签: pythondelegationgetattrslotssetattr

解决方案


更合适的方法是在委托之前检查属性名称是否在__slots__类层次结构中的任何可用中:

class BCorrect(object):
    __slots__ = ('b',)

    def __init__(self, b) -> None:
        self.b = b

    def _in_slots(self, attr) -> bool:
        for cls in type(self).__mro__:
            if attr in getattr(cls, '__slots__', []):
                return True
        return False

    def __getattr__(self, attr):
        if self._in_slots(attr):
            return object.__getattr__(self, attr)
        return getattr(self.b, attr)

    def __setattr__(self, attr, value):
        if self._in_slots(attr):
            object.__setattr__(self, attr, value)
            return
        setattr(self.b, attr, value)

这样做的好处是不会破坏继承,也不需要在__init__.


推荐阅读