短绒棉和我的一些课似乎在幕后发生。我有几个类的实例属性太多(一种情况下最多26个)。我可以通过将一些相关属性组合到新类中来解决该问题。但是,该方法不适用于最新课程。目前,所有属性都是基元,我可以很容易地将它们分成相关的组。令人反感的类如下:
class BigClass():
"""
This class has too many (16/8) instance attributes!
"""
a = float(0)
b = float(0)
c = float(0)
d = float(0)
e = float(0)
f = float(0)
g = float(0)
h = float(0)
i = float(0)
j = float(0)
k = float(0)
l = float(0)
m = float(0)
n = float(0)
o = float(0)
p = float(0)
然后我将其分为两类,将适当的属性分组在一起。
class SmallClass1():
"""
This class is smaller, it has 8/8 attributes.
It is a logical grouping of 8 attributes that were
previously found in BigClass()
"""
a = float(0)
b = float(0)
c = float(0)
d = float(0)
e = float(0)
f = float(0)
g = float(0)
h = float(0)
class SmallClass2():
"""
This class is smaller, it has 8/8 attributes.
It is a logical grouping of 8 attributes that were
previously found in BigClass()
"""
i = float(0)
j = float(0)
k = float(0)
l = float(0)
m = float(0)
n = float(0)
o = float(0)
p = float(0)
class NewBigClass():
"""
This class still has too many (16/8) instance attributes!
"""
grouping1 = SmallClass1()
grouping2 = SmallClass2()
但是,lint仍然声称NewBigClass()声明中的属性太多。 我是否缺少某些东西,也许是对该警告要避免的核心理解? 我不确定为什么我能解决一堂课,但不是全部解决。它们都遵循相似的布局。
感谢您的帮助。
答案 0 :(得分:0)
我已经解决了问题。
在重构类以减少实例属性的数量时,我先运行了pylint,然后在整个项目中完全重构了该类。这意味着当我分解class.a,class.b等时,我仍在为项目中的其他位置分配class.a。
修复这些引用后,我的pylint警告已修复。
感谢所有贡献者。