如何告诉pylint类实例变量不是常量

2024-06-26 01:47:45 发布

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

class MyFavoriteClass():
  def __init__(self):
      self.counter = 0
  def memberFunction(self):
      self.counter = self.counter + 1

myinstance = MyFavoriteClass() #Pylint complains here
myinstance.memberFunction()

pylint报告的第二行到最后一行的错误是

^{pr2}$

我已经读到完全禁用这种类型的错误是可能的,但是有必要吗?在

我怎样才能告诉pylint myinstance不是常量?在

pylint-v报告的系统配置

pylint 0.26.0, 
astng 0.24.1, common 0.59.1
Python 2.7.5+ (default, Sep 19 2013, 13:48:49) 
[GCC 4.8.1]

Tags: selfhereinitdef报告错误counterclass
1条回答
网友
1楼 · 发布于 2024-06-26 01:47:45

使用pylint,您可以通过使用格式为# pylint: disable={comma-separated-list-of-names-or-codes}的特殊注释来忽略行上的单个错误、警告等

class MyFavoriteClass():
    def __init__(self):
        self.counter = 0
    def memberFunction(self):
        self.counter = self.counter + 1

myinstance = MyFavoriteClass() # pylint: disable=invalid-name
myinstance.memberFunction()

或者,可以在pylint配置文件中指定要忽略的消息代码列表:

^{pr2}$

注意:从配置文件中禁用代码将全局忽略它。如果你想在某一行忽略它,你必须使用开头提到的特殊注释。在

pylint配置文件通过首先检查PYLINTRC环境变量来确定。如果它不提供文件,那么~/.pylintrc和{}将被连续检查。在

如果您可以控制正在执行的pylint命令,那么还可以使用 rcfile参数指定配置。在

如果要生成示例配置,请运行:

pylint  generate-rcfile

另外,如果您禁用一个代码,它将触发^{},它本身也可以被禁用(最好是在配置中)。在

相关问题 更多 >