AttributeError:首选项实例没有属性“ __setattr__”

时间:2019-04-11 09:52:08

标签: python json linux attributeerror

我的应用程序在Windows上运行,我正在尝试为Linux配置它。

我得到以下AttributeError

  

首选项实例没有属性'self。 setattr '

class Preferences:

    def __init__(self):
        """
        Default preferences are imported from parameters 
        file at each creation of an object.
        The database default is not automatically 
        updated (file schema.sql). On Preferences parameters change,
        schema.sql should be changed accordingly.
        """
        with open(os.path.join(APP_ROOT, 'parameters.json')) as parameters:
            json_data = json.loads(parameters.read())

        # for each att(ribute) from preference file, 
        # create an attribute in Preferences Object 
        # which is a Preference object
        for att in json_data['preferences']:
          self.__setattr__(
            att,
            Preference(
              **{label: json_data['preferences'][att][label] for label in json_data['preferences'][att]}))

2 个答案:

答案 0 :(得分:0)

您的类实际上没有__setattr__方法。你想做的是

setattr(self, name, value)

代替

self.__setattr__(name, value)

答案 1 :(得分:0)