如何处理Pylint的“太多实例属性”信息? [英] How to deal with Pylint's "too-many-instance-attributes" message?

查看:328
本文介绍了如何处理Pylint的“太多实例属性”信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚尝试用Pylint整理一些代码,最后剩下的错误是

I have just tried to lint some code with Pylint, and the last remaining error is

R0902: too-many-instance-attributes (8/7)

我了解限制实例属性数量的基本原理,但是七个似乎有点低。我也意识到,短绒棉布应该没有硬道理。但是,我想知道我应该怎么做,而不是:

I understand the rationale behind limiting the number of instance attributes, but seven seems a bit low. I also realise that the linter should not have the last word. However, I would like to know what I should be doing instead of:

def __init__(self, output_file=None, output_dir=None):
    """
    Set the frobnicator up, along with default geometries
    """

    self.margin = 30

    self.pos = [0, 0]
    self.sep = [5, 5]

    self.cell = [20, 20]

    self.frobbr = library.Frobbr()

    page = self.frobbr.get_settings('page')

    self.lim = [page.get_width() - self.margin,
                page.get_height() - self.margin]

    self.filename = output_file
    self.moddir = output_dir

我应该将几何图形打包成字典,还是要做其他事情来阻止Pylint抱怨,或者只是忽略它(我不是很想做)? / p>

Should I package the geometries up into a dict, do something else to stop Pylint complaining, or just ignore it (which I don't really want to do)?

推荐答案

一个linter的工作是使您意识到代码的潜在问题,并且正如您在问题中所说的那样, H

A linter's job is to make you aware of potential issues with your code, and as you say in your question, it should not have the last word.

如果您已经考虑了pylint所说的话,并决定对于这一堂课,您拥有的属性是适当的(对我来说似乎很合理) ,您既可以抑制该错误,又可以通过在类中添加禁用注释来表明您已经考虑了该问题:

If you've considered what pylint has to say and decided that for this class, the attributes you have are appropriate (which seems reasonable to me), you can both suppress the error and indicate that you've considered the issue by adding a disabling comment to your class:

class Frobnicator:

    """All frobnication, all the time."""

    # pylint: disable=too-many-instance-attributes
    # Eight is reasonable in this case.

    def __init__(self):
        self.one = 1
        self.two = 2
        self.three = 3
        self.four = 4
        self.five = 5
        self.six = 6
        self.seven = 7
        self.eight = 8

那样,您既不会忽略Pylint也不是它的奴隶;

That way, you're neither ignoring Pylint nor a slave to it; you're using it as the helpful but fallible tool it is.

默认情况下,当您在本地禁用支票时,Pylint会生成一条信息性消息:

By default, Pylint will produce an informational message when you locally disable a check:

 Locally disabling too-many-instance-attributes (R0902) (locally-disabled)

您可以通过以下两种方式之一阻止那个消息出现:

You can prevent that message from appearing in one of two ways:


  1. 在运行pylint时添加 disable = 标志:

$ pylint --disable=locally-disabled frob.py 


  • 将指令添加到 pylintrc 配置文件:

    [MESSAGES CONTROL]
    disable = locally-disabled
    


  • 这篇关于如何处理Pylint的“太多实例属性”信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆