4
在Python中,带有一个下划线的前缀表示不应该在其类外部访问成员。这似乎是按照类别,如Java和C++。Pylint W0212保护访问
但是,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的描述消息here。
鸭子打字和__eq__重载可能是危险的。我建议也测试self .__ class__ == other .__ class__ –