跳到内容

redefined-slots-in-subclass (PLW0244)

源自 Pylint 代码检查工具。

此规则不稳定且处于预览状态。使用需要 --preview 标志。

作用

检查子类中重新定义的槽。

为什么这不好?

如果一个类定义了一个基类中也定义的槽,则基类槽定义的实例变量将无法访问(除非直接从基类检索其描述符)。

示例

class Base:
    __slots__ = ("a", "b")


class Subclass(Base):
    __slots__ = ("a", "d")  # slot "a" redefined

建议改为

class Base:
    __slots__ = ("a", "b")


class Subclass(Base):
    __slots__ = "d"