Pylint 说:W0233:来自非直接基 class 'Nested' 的 __init__ 方法被调用(非父初始化调用)

Pylint says: W0233: __init__ method from a non direct base class 'Nested' is called (non-parent-init-called)

我是编程新手 python 也是。当我尝试将我的数据结构包装到 class 以便不使 listdict 迭代时,我收到 pylint 错误消息:

W0233: __init__ method from a non direct base class 'Nested' is called (non-parent-init-called)

有什么最好的 "pythonic" 方法吗?

我的json数据是这样的:

{
    "template"  :   [
        {
            "folder"    :   "/Users/SA/Documents/GIT/rs-finance/templates",
            "basetpl"   :   "tpl.docx",
            "header"    :   "header_tpl.docx",
            "table"     :   "table_tpl.docx", 
            "footer"    :   "footer_tpl.docx"
        }
    ],
    "export"    :   [
        {
            "folder"    :   "/Users/SA/Documents/GIT/rs-finance/export",
            "name"      :   "result.docx"
        }
    ]
}

当我将此数据(或其片段)加载到 dictlist 变量并尝试用此 class:

包装它时
class Nested ():
    def __init__(self, data):
        if isinstance (data, dict):
            for key, value in data.items():
                if isinstance(value, (float, int, str)):
                    setattr(self, key, value)
                else:
                    setattr(self, key, Nested(value))
        if isinstance(data, list):
            for item in data:
                self.__init__(item)

Pylint 不喜欢我的最后一行

显式调用 __init__ 并没有错,但它 奇怪 ,这就是 Pylint 警告 你的全部.

更好的做法是编写一个单独的递归函数来执行您想要的操作,然后从 __init__.

调用它
class Nested:
    def __init__(self, data):
        self.recursive(data)

    def recursive(self, data):
        if isinstance(data, dict):
            for key, value in data.items():
                if isinstance(value, (float, int, str)):
                    setattr(self, key, value)
                else:
                    setattr(self, key, Nested(value))
        elif isinstance(data, list):
            for item in data:
                self.recursive(item)