【发布时间】:2016-02-29 14:05:27
【问题描述】:
在 Python 中,带有一个下划线的前缀表示不应在其类之外访问成员。这似乎是基于每个班级的,例如 Java 和 C++。
但是,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。
【问题讨论】:
-
duck 打字和 eq 重载可能很危险。我还建议测试 self.__class__ == other.__class__
-
相关(但与 Pycharm):stackoverflow.com/questions/42736044/…
标签: python private-members pylint