我们将一些特定的功能实现为 Python 类,以便继承它的开发人员可以轻松扩展。每个类都有一个带有项目列表的内部 Config 类。基类有一个空的 Config 类,每个继承类都在其中定义了一些项。然后每次使用 Config 子类的项时 pylint 都会抱怨。
例如,这段代码:
class A(object):
class Config(object):
def __init__(self):
self.item1 = 1
self.item2 = 2
def __init__(self):
self._config = self.Config()
class B(A):
class Config(A.Config):
def __init__(self):
super(B.Config, self).__init__()
self.item3 = 3
self.item4 = 4
def do_something(self):
if self._config.item3 == 3:
print 'hello'
if self._config.item1 == 5:
print 'bye'
然后您可以将其用作:
>>> B().do_something()
hello
我们的程序运行良好,背后的想法相同。问题是每次我们使用这些物品时 pylint 都会继续抱怨。例如,在这种情况下它会说:
E1101(no-member) Instance of 'Config' has no 'item3' member
所以我的问题是“如何避免这些 pylint 警告而不禁用它们?有没有更好的方法来实现我想要的?请注意,在实际程序中,config 值会根据用户数据而变化,并且它不是一堆常量。
多谢。
在我看来,这似乎过于复杂。使用字典进行配置会很好。例如:
class A(object):
def __init__(self):
self._config = {
'item1': 1,
'item2': 2,
}
class B(A):
def __init__(self):
super(B, self).__init__()
self._config.update({
'item3': 3,
'item4': 4,
})
def do_something(self):
if self._config['item3'] == 3:
print 'hello'
if self._config['item1'] == 5:
print 'bye'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)