我一直在研究一个具有属性的类,但是我们遇到了一个 pylint (0.25.1) 的讨厌问题在下面的代码中,我们定义了一个具有在 python 2.6 中引入的属性的类但是,pylint 抱怨说在__init__
方法self.aProperty
中将覆盖已定义的名为 aProperty 的方法。我还粘贴了控制台的输出和 pylint 消息的输出。
这是“请向 pylint 开发人员报告”的情况,还是这段(示例)代码错误?
"""example module"""
class Example(object):
"""example class"""
@property
def aProperty(self):
"""get"""
print "using getter"
return self._myPropertyValue
@aProperty.setter
def aProperty(self, value):
"""set"""
print "using setter"
self._myPropertyValue = value
def secondPublicMethodToIgnorePylintWarning(self):
"""dummy"""
return self.aProperty
def __init__(self):
"""init"""
self._myPropertyValue = None
self.aProperty = "ThisStatementWillRaise E0202"
anExample = Example()
print anExample.aProperty
anExample.aProperty = "Second binding"
print anExample.aProperty
控制台输出:
使用 setter
使用 getter
ThisStatementWillRaise E0202
using setter
using getter
第二个绑定
皮林特输出:
E0202:7,4:Example.aProperty:在 test1 第 26 行中受影响的属性隐藏此方法
E0202:13,4:Example.aProperty:在 test1 第 26 行中受影响的属性隐藏此方法