Pylint E0202假阳性?还是这段代码错了?

2024-10-02 02:27:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在研究一个带有属性的类,但是在下面的代码中我们遇到了一个pylint(0.25.1)的严重问题,我们正在用python2.6中引入的一个属性来定义一个类 然而,pylint抱怨说在__init__方法中self.aProperty将重写名为property的已定义方法。我还粘贴了控制台的输出和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

控制台输出:

using setter
using getter
ThisStatementWillRaise E0202
using setter
using getter
Second binding

Pylint输出:

E0202: 7,4:Example.aProperty: An attribute affected in test1 line 26 hide this method
E0202: 13,4:Example.aProperty: An attribute affected in test1 line 26 hide this method


Tags: 代码self属性initexampledefpylintsetter

热门问题