![](/img/trans.png)
[英]AttributeError: Preferences instance has no attribute '__setattr__'
[英]python: setattr AttributeError: object has no attribute
我有一個 object self.config 有一些變量,我想首先檢查 self.config 中是否存在變量('db_password'),如果存在,然后修改變量值,例如self.config.db_password = 'modified values'
。 以下是 python 代碼
if hasattr(self.config, enc_variable):
setattr(self.config, enc_variable, f.decrypt(self.config.enc_variable.encode('ascii')).decode('ascii'))
AttributeError:“配置”object 沒有屬性“enc_variable”
這部分是錯誤的:
self.config.enc_variable
它應該是
getattr(self.config, enc_variable)
或者
self.config.db_password
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.