Python Pylint W0212受保护访问

Python Pylint W0212受保护访问,python,private-members,pylint,Python,Private Members,Pylint,在Python中,带有一个下划线的前缀表示不应在其类之外访问成员。这似乎是基于每个类,如和 然而,pylint似乎在每个对象的基础上执行此约定。有没有一种方法可以允许每类访问而不必求助于#pylint:disable=protected access class A: def __init__(self): self._b = 5 def __eq__(self, other): return self._b == other._b 结果: p

在Python中,带有一个下划线的前缀表示不应在其类之外访问成员。这似乎是基于每个类,如和

然而,pylint似乎在每个对象的基础上执行此约定。有没有一种方法可以允许每类访问而不必求助于
#pylint:disable=protected access

class A:
    def __init__(self):
        self._b = 5

    def __eq__(self, other):
        return self._b == other._b
结果:

pylint a.py
a.py:6: W0212(protected-access) Access to a protected member _b of a client class

Pylint描述了该消息。

Pylint不知道是哪种类型的
other
(应该如何进行,您可以将A的实例与所有内容进行比较),因此发出警告。我不认为有办法解除警告


您可以仅在该行中添加
#pylint:disable=W0212
来禁用该行的警告。

Christian Geier关于出现错误的原因以及如何禁用它的说法是正确的

我鼓励你考虑改变你的代码:虽然皮利特告诉你一些重要的事情。从您的示例代码来看,您似乎希望使用eq将类A的对象与类A的其他对象进行比较,但您的示例不能保证调用方不会尝试
A()==C()
。选中圆()时返回
True
。\u radius==Sphere.\u radius可能会导致问题

有关如何处理此问题的讨论,请参阅。

duck类型和eq重载可能很危险。我建议也测试self.\uuuuu class\uuuuuu==other.\uuuuuu class\uuuuu相关(但与Pycharm相关):另请参见.Or
\pylint:disable=protectedaccess