简体   繁体   English

python:setattr AttributeError:object 没有属性

[英]python: setattr AttributeError: object has no attribute

I have an object self.config which has some variables, I would like to first check if a variable ('db_password') exist in self.config and if it exists then modify the variable value something like self.config.db_password = 'modified values' .我有一个 object self.config 有一些变量,我想首先检查 self.config 中是否存在变量('db_password'),如果存在,然后修改变量值,例如self.config.db_password = 'modified values' following is the python code以下是 python 代码

    if hasattr(self.config, enc_variable):
        setattr(self.config, enc_variable, f.decrypt(self.config.enc_variable.encode('ascii')).decode('ascii'))

AttributeError: 'Config' object has no attribute 'enc_variable' AttributeError:“配置”object 没有属性“enc_variable”

This part is buggy:这部分是错误的:

self.config.enc_variable

It should be它应该是

getattr(self.config, enc_variable)

Or或者

self.config.db_password

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:首选项实例没有属性“ __setattr__” - AttributeError: Preferences instance has no attribute '__setattr__' Python AttributeError: Object 没有属性 - Python AttributeError: Object has no attribute AttributeError: 'NoneType' 对象没有属性 'findAll' - Python - AttributeError: 'NoneType' object has no attribute 'findAll' - Python Django和Python AttributeError:“零售商”对象没有属性 - Django & Python AttributeError: 'Retailer' object has no attribute Python AttributeError:对象在QGIS插件中没有属性 - Python AttributeError : Object has no attribute in QGIS Plugin Python AttributeError:“模块”对象没有“连接”属性 - Python AttributeError: 'module' object has no attribute 'connect' Python 类( AttributeError: '' object has no attribute '') - Python Classes ( AttributeError: '' object has no attribute '') Python:AttributeError:'NoneType'对象没有属性'rfind' - Python: AttributeError: 'NoneType' object has no attribute 'rfind' Python:AttributeError:'module'对象没有属性'AddReference'? - Python: AttributeError: 'module' object has no attribute 'AddReference'? Python AttributeError:'str'对象没有属性'DataFrame' - Python AttributeError: 'str' object has no attribute 'DataFrame'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM