I think you could implement it like this:<p><pre><code> from collections import defaultdict
class D(defaultdict):
def __init__(self):
super(D, self).__init__(D)
__getattr__ = defaultdict.__getitem__
__setattr__ = defaultdict.__setitem__
__repr__ = dict.__repr__</code></pre>
One of the best things about the Python ecosystem is the acceptance and idealization of "Pythonic" solutions. While I acknowledge and praise the authors effort in making this complete and correct, the aim itself is misguided because it is grossly unpythonic. I reject it and hope it does not see significant use.
This doesn't even come close to correctly implementing the MutableMapping interface in a way that makes sense for the expected behaviour. The biggest problem I see is that it's not at all clear how methods like __iter__, key/value (and the view variants) and has_key should behave. Honestly, I'd homebrew a nested defaultdict(defaultdict) every time because at least I know what it'll do.
Hey guys, author of addict here. Would appreciate immensely if you would file issues of problems you find. It's a very new project, that I cooked up after work hours this week, and any input is good input! Thanks.
Aaron Swartz did something like this years ago, except it's ultra simple and doesn't include any of the weird recursive stuff, in his web.py: the Storage class.<p><a href="https://github.com/webpy/webpy/blob/master/web/utils.py#L52" rel="nofollow">https://github.com/webpy/webpy/blob/master/web/utils.py#L52</a>
So I've made some changes over the day. Some issues still stand, but as some people pointed out it was unnecessary to store both attributes and items. Now the get and set attribute simply calls get and set items. If you find the time, feel free to add an issue along with you HN comment :-)
That's funny, I'm working on something similar for Javascript: <a href="https://www.npmjs.com/package/xs.js" rel="nofollow">https://www.npmjs.com/package/xs.js</a>
I use some similar classes in my projects.<p>It always has frustrated me to see that attribute- and dictionary access is so similar but still different. Of course in most cases, there are some good reasons to have it different -- but sometimes there are also good reasons to generalize access to some important values.<p>So, we have duck typing and car typing. And both together we have a duck-car, because it quacks like a duck and runs like a car.
This looks like something I really wish was just baked into the language. I think there is a reason it isn't, like in JavaScript, and that is because Python isn't as weakly typed, and encourages explicitness. So, I wouldn't use it and would discourage everyone working with me from using this.
<a href="https://github.com/pjkundert/cpppo/blob/master/dotdict.py" rel="nofollow">https://github.com/pjkundert/cpppo/blob/master/dotdict.py</a><p>Quite complete attribute accessible dict, with iteration by full-depth keys. Very useful for human readable output.
this is an idea that I've seen used many times in the past.
One of the first refs I saw to this mashup of the keys of a dict and the values in __dict__ was on Activestate in 2006 or so. There are also multiple versions on Stackoverflow.<p>I'm reminded of the web designer example here (<a href="https://gist.github.com/fmeyer/289467" rel="nofollow">https://gist.github.com/fmeyer/289467</a>) line 89.<p>I know that people use github for many things, including tracking their config files, etc. However, this seems to be a post by the owner of the repo. Would you ever, I mean _ever_ be happy if someone required this sort of 'library' as a dependency in a project? I know I would not.
I've been using a similar thing I call Struct for ages, as part of my utils package:<p><a href="https://github.com/rcarmo/python-utils/blob/master/core.py" rel="nofollow">https://github.com/rcarmo/python-utils/blob/master/core.py</a><p>Can't really see this as newsworthy, sorry.<p>Edited to add: didn't mean to sound mean (if you'll pardon the pun), just factual. There are ActiveState recipes out there for this sort of thing since the beginning of time... Also, check my other comments for thoughts on syntax and immutability.
Maybe consider creating a pull request against the standard python library. This is really nice. This feels like it belongs in the collections module alongside defaultdict.
why do people keep insisting on inventing this ugly and pointless 'solution'? too much Javascript?<p>just use `mydict.setdefault()` and `defaultdict` and give up on the whole attr access thing. it's a dict