AttributeError: Preferences instance has no attribute '__setattr__'

AttributeError: Preferences instance has no attribute '__setattr__'

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

我得到以下 AttributeError:

Preferences instances has no attribute '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]}))

您的 class 确实没有 __setattr__ 方法。你要做的是

setattr(self, name, value)

而不是

self.__setattr__(name, value)

使用setattr代替__setattr__

https://docs.python.org/3/library/functions.html#setattr