Dictionary object that can also be accessed via attributes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import sys
from collections import MutableMapping
class AttrDict(MutableMapping):
"""Dict-like object that can be accessed by attributes
>>> obj = AttrDict()
>>> obj['test'] = 'hi'
>>> print obj.test
hi
>>> del obj.test
>>> obj.test = 'bye'
>>> print obj['test']
bye
>>> print len(obj)
1
>>> obj.clear()
>>> print len(obj)
0
"""
def __init__(self, *args, **kwargs):
self.__dict__.update(*args, **kwargs)
def __getitem__(self, key):
return self.__getattribute__(key)
def __setitem__(self, key, val):
self.__setattr__(key, val)
def __delitem__(self, key):
self.__delattr__(key)
def __iter__(self):
return iter(self.__dict__)
def __len__(self):
return len(self.__dict__)
if __name__ == '__main__':
import doctest
doctest.testmod()
|
This container object allows both key and attribute access while implementing all of the dict methods.
This appears to do the same thing - at the very least, it passes the doctest - with less boilerplate.
For a small performance hit, one can expose the full dict interface:
I suppose if I actually test the code, I might have fewer bugs:
I think @Ryan has the right idea about the better way to do something like this, but feel a more "canonical" implementation would be this way:
I've also seen this recipe called
JSObject
because it creates Python objects that act like Javascript objects.For anyone interested, a similar package is now available from PyPI.