有关继承的嵌套类成员的Pylint警告 [英] Pylint warnings on inherited nested class members

查看:255
本文介绍了有关继承的嵌套类成员的Pylint警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一些作为Python类实现的特定功能,以便我们的开发人员可以轻松地继承继承它.每个类都有一个内部的Config类,其中包含项目列表.基类有一个空的Config类,每个继承类都在其中定义了一些项目.然后每次使用Config子类的项目时pylint都会抱怨.

We have some particular functionality implemented as a Python class, in order to be easily extended by our developers inheriting it. Each class has a an inner Config class with a list of items. The base class has an empty Config class, and each inheriting class defines some items into it. Then pylint complains each time the item of Config subclass is used.

例如,此代码:

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都会继续抱怨.例如,在这种情况下,它会说:

Our program works nicely with the same idea behind. Thing is pylint continues complaining each time we use the items. For example, in this case it would say:

E1101(no-member) Instance of 'Config' has no 'item3' member

因此,我的问题是:如何在不禁用这些警告的情况下避免这些警告?有没有更好的方法来实现我想要的?请注意,在实际程序中,配置值根据用户数据而变化,并且不是一堆常量.

So my question is ¿how can I avoid these pylint warnings, without disabling them? ¿is there a better way to achieve what I want? Note that in the real program, config values change according to user data, and it is not a bunch of constants.

非常感谢.

推荐答案

这似乎是复杂的IMO.使用dict进行配置会很好.例如:

This seems over complicated IMO. Using a dict for config will do nicely. e,g:

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'

这篇关于有关继承的嵌套类成员的Pylint警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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