简体   繁体   中英

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' . following is the python code

    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'

This part is buggy:

self.config.enc_variable

It should be

getattr(self.config, enc_variable)

Or

self.config.db_password

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM