vector8188
vector8188

Reputation: 1393

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'

Upvotes: 1

Views: 715

Answers (1)

dataista
dataista

Reputation: 3457

This part is buggy:

self.config.enc_variable

It should be

getattr(self.config, enc_variable)

Or

self.config.db_password

Upvotes: 1

Related Questions