Pylint W0212 保护访问 [英] Pylint W0212 protected-access
问题描述
在 Python 中,以一个下划线作为前缀表示不应在其类之外访问成员.这似乎是基于每个类的,例如 Java 和 C++.
In Python, prefixing with one underscore indicates that a member should not be accessed outside of its class. This seems to be on a per-class basis like Java and C++.
但是,pylint 似乎在每个对象的基础上强制执行此约定.有没有办法允许每个类访问而不诉诸 #pylint: disable=protected-access
?
However, pylint seems to enforce this convention on a per-object basis. Is there a way to allow per-class access without resorting to #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 describes the message here.
推荐答案
pylint 不知道 other
是哪种类型(应该怎么做,你可以将 A 的实例与所有内容进行比较),因此警告.我认为没有办法禁用警告.
pylint doesn't know of which type other
is (how should it, you can compare an instance of A to everything), therefore the warning. I don't think there is a way around disabling the warning.
您可以通过将 # pylint: disable=W0212
附加到该行来禁用该行的警告.
You can disable the warning for only that one line with appending # pylint: disable=W0212
to that line.
这篇关于Pylint W0212 保护访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!