python - AttributeError:首选项实例没有属性 '__setattr__'

标签 python json linux attributeerror

我的应用程序在 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]}))

最佳答案

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

setattr(self, name, value)

代替

self.__setattr__(name, value)

关于python - AttributeError:首选项实例没有属性 '__setattr__',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55629611/

相关文章:

python - 如何强制zlib解压超过X字节?

linux - 提取分区数的最佳方法?

python - 循环遍历字符串并仅返回某些字符。 Python

python - 分发具有 Julia 依赖项的独立 Python 软件

python - 用于根据提交的字符串推断国家/地区的库或 API?

javascript - KnockoutJS 按特定属性过滤

android - 解析 JSON 时出现 NullPointer 异常

PHP 丢失 MySQL 结果的字段数据类型(一切都变成字符串)

python - 导入错误 : No module named "user_models"

linux - Linux下的ps命令