【问题标题】:Why does Pylint error on metaclass-defined attribute usage?为什么 Pylint 在元类定义的属性使用上出错?
【发布时间】:2016-10-06 07:37:25
【问题描述】:

我在我的代码中使用了一个元类,并且代码有效。 (使用元类,它在对象创建时将属性test_attr 设置为"Success!"。)但是,当我在此代码上运行pylint 时,它在Test.test 中显示错误,说test_attr 未定义。

class MyMeta(type):
    def __new__(mcs, name, bases, attrs):
        attrs["test_attr"] = "Success!"
        return super().__new__(mcs, name, bases, attrs)


class Test(metaclass=MyMeta):
    def test(self):
        return self.test_attr

我应该怎么做才能满足pylint?是否有解决此问题的配置选项?我的代码有什么需要修复的地方吗?

【问题讨论】:

    标签: python python-3.x attributes metaclass pylint


    【解决方案1】:

    我猜 pylint 根本不懂这种“魔法”——你为什么要这样做?

    您可以设置generated-membersignored-classes(参见docs)来告诉pylint。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-01
      • 2015-07-06
      • 2020-03-03
      • 2013-05-10
      • 2011-09-17
      • 2020-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多