Python 如何处理 Pylint 的 "too-many-instance-attributes" 消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24434510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to deal with Pylint's "too-many-instance-attributes" message?
提问by Inductiveload
I have just tried to lint some code with Pylint, and the last remaining error is
我刚刚尝试用 Pylint 整理一些代码,最后剩下的错误是
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:
我理解限制实例属性数量背后的基本原理,但七个似乎有点低。我也意识到 linter 不应该有最后的决定权。但是,我想知道我应该做什么而不是:
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
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)?
我应该将几何图形打包成一个字典,做其他事情来阻止 Pylint 抱怨,还是只是忽略它(我真的不想这样做)?
采纳答案by Zero Piraeus
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.
linter 的工作是让您意识到代码的潜在问题,正如您在问题中所说,它不应该是最终决定权。
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:
如果您已经考虑过 pylint 必须说的内容并决定对于这个类,您拥有的属性是合适的(这对我来说似乎是合理的),您既可以抑制错误,也可以通过添加禁用来表明您已经考虑过这个问题给你的班级评论:
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
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:
默认情况下,当您在本地禁用检查时,Pylint 将生成一条信息性消息:
Locally disabling too-many-instance-attributes (R0902) (locally-disabled)
You can prevent thatmessage from appearing in one of two ways:
您可以通过以下两种方式之一阻止该消息出现:
Add a
disable=
flag when running pylint:$ pylint --disable=locally-disabled frob.py
Add a directive to a
pylintrc
config file:[MESSAGES CONTROL] disable = locally-disabled
disable=
运行pylint时添加一个标志:$ pylint --disable=locally-disabled frob.py
在
pylintrc
配置文件中添加一条指令:[MESSAGES CONTROL] disable = locally-disabled
回答by NChauvat
The answer from Zero Piraeus is a good one. That said, since you provide little context to your initmethod, not even a real class name, it is difficult to be affirmative, but I would say filename and moddir have nothing to do aside margin, position, etc.
零比雷埃夫斯的答案很好。也就是说,由于您为init方法提供的上下文很少,甚至没有提供真正的类名,因此很难肯定,但我会说 filename 和 moddir 除了边距、位置等之外没有任何关系。
IO operations are often best isolated into functions rather than put into methods. Their are often many different formats and serialization options, and most of the time you do not want to mix them with your object logic (methods). It is easier to add a new IO function that takes some file, string, blob or whatever and returns the object encoded into it than it is to maintain an object that has many methods that handle many different IO operations.
IO 操作通常最好隔离在函数中而不是放入方法中。它们通常有许多不同的格式和序列化选项,大多数时候您不想将它们与您的对象逻辑(方法)混合在一起。添加一个新的 IO 函数,它接受一些文件、字符串、blob 或任何东西并返回编码到其中的对象,比维护一个具有许多处理许多不同 IO 操作的方法的对象更容易。
回答by Ian
This is an ideological objection, but personally I tend to try to make these kind of changes as universal as possible. If 7 is not enough instances in one file, and I choose to allow it here, why not everywhere? I don't always make changes to the lint rules across the board, but I at least consider it. To that end, if you want to make an across the board change, in your .pylintrc file change max-attributes=7
in the DESIGN
section.
这是一种意识形态上的反对,但我个人倾向于尝试使这些变化尽可能普遍。如果在一个文件中 7 个实例不够,而我选择在此处允许它,为什么不在所有地方都允许?我并不总是全面更改 lint 规则,但至少我会考虑。为此,如果你想打一个全线的变化,在你的.pylintrc文件变化max-attributes=7
的DESIGN
部分。
Since I think 7 is a little low across the board, I changed:
由于我认为 7 全线有点低,所以我改变了:
[DESIGN]
max-attributes=7
to
到
max-attributes=12
回答by Acumenus
I would disable this message altogether by adding too-many-instance-attributes
to the project's pylintrc
or .pylintrc
file as in the example below:
我将通过添加too-many-instance-attributes
到项目pylintrc
或.pylintrc
文件中来完全禁用此消息,如下例所示:
[MESSAGES CONTROL]
disable=
locally-disabled,
locally-enabled,
logging-format-interpolation,
no-init,
too-few-public-methods,
too-many-instance-attributes, # <-- Ensure at least this entry.
fixme