For this purpose, many years ago, I came up with the simple idiom Bunch ; One easy way to implement Bunch :
class Bunch(object): def __init__(self, adict): self.__dict__.update(adict)
If config is a dict, you cannot use config.account_receivable - it is absolutely impossible because dict does not have this attribute, period. However, you can wrap config in Bunch :
cb = Bunch(config)
and then access cb.config_account on your heart content!
Edit : if you want the assignment of attributes to Bunch also affect the original dict ( config in this case) so that, for example, cb.foo = 23 will execute config['foo'] = 23 , you will need a small implementation of Bunch :
class RwBunch(object): def __init__(self, adict): self.__dict__ = adict
Usually a regular Bunch is preferred, precisely because after creating the instance of Bunch and dict it was βprimedβ from completely untied ones - changes in any of them do not affect the others; and such a denouement is usually desired.
When you want communication effects, then RwBunch is the way to get them: with it, each parameter or deleting an attribute in an instance will internally set or remove an element from a dict , and, conversely, setting or deleting elements from a dict will internally set or remove attributes from an instance .
source share