[英]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.