TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: Addict – a Python dict whos values can be get and set using attributes

80 pointsby mewwtsover 10 years ago

18 comments

tyrionover 10 years ago
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>
评论 #8745158 未加载
评论 #8745160 未加载
评论 #8745208 未加载
评论 #8745335 未加载
评论 #8744992 未加载
nvaderover 10 years ago
One of the best things about the Python ecosystem is the acceptance and idealization of &quot;Pythonic&quot; 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.
评论 #8745020 未加载
ecmaover 10 years ago
This doesn&#x27;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&#x27;s not at all clear how methods like __iter__, key&#x2F;value (and the view variants) and has_key should behave. Honestly, I&#x27;d homebrew a nested defaultdict(defaultdict) every time because at least I know what it&#x27;ll do.
评论 #8744910 未加载
mewwtsover 10 years ago
Hey guys, author of addict here. Would appreciate immensely if you would file issues of problems you find. It&#x27;s a very new project, that I cooked up after work hours this week, and any input is good input! Thanks.
评论 #8745207 未加载
评论 #8744934 未加载
评论 #8745083 未加载
lifeisadanceover 10 years ago
Aaron Swartz did something like this years ago, except it&#x27;s ultra simple and doesn&#x27;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:&#x2F;&#x2F;github.com&#x2F;webpy&#x2F;webpy&#x2F;blob&#x2F;master&#x2F;web&#x2F;utils.py#L52</a>
评论 #8745398 未加载
评论 #8745449 未加载
mewwtsover 10 years ago
So I&#x27;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 :-)
Argorakover 10 years ago
A pleasure to see the Python community adopting Rubys drug-related naming schemes.
评论 #8744911 未加载
评论 #8744909 未加载
评论 #8744912 未加载
phazeliftover 10 years ago
That&#x27;s funny, I&#x27;m working on something similar for Javascript: <a href="https://www.npmjs.com/package/xs.js" rel="nofollow">https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;xs.js</a>
评论 #8746345 未加载
PythonicAlphaover 10 years ago
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.
Demiurgeover 10 years ago
This looks like something I really wish was just baked into the language. I think there is a reason it isn&#x27;t, like in JavaScript, and that is because Python isn&#x27;t as weakly typed, and encourages explicitness. So, I wouldn&#x27;t use it and would discourage everyone working with me from using this.
pjkundertover 10 years ago
<a href="https://github.com/pjkundert/cpppo/blob/master/dotdict.py" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;pjkundert&#x2F;cpppo&#x2F;blob&#x2F;master&#x2F;dotdict.py</a><p>Quite complete attribute accessible dict, with iteration by full-depth keys. Very useful for human readable output.
RayVRover 10 years ago
this is an idea that I&#x27;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&#x27;m reminded of the web designer example here (<a href="https://gist.github.com/fmeyer/289467" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;fmeyer&#x2F;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 &#x27;library&#x27; as a dependency in a project? I know I would not.
评论 #8745831 未加载
rcarmoover 10 years ago
I&#x27;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:&#x2F;&#x2F;github.com&#x2F;rcarmo&#x2F;python-utils&#x2F;blob&#x2F;master&#x2F;core.py</a><p>Can&#x27;t really see this as newsworthy, sorry.<p>Edited to add: didn&#x27;t mean to sound mean (if you&#x27;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.
评论 #8744896 未加载
cjbprimeover 10 years ago
Hey, this looks super useful. Thanks for posting!
评论 #8744904 未加载
personjerryover 10 years ago
I thought AdDict would be a dictionary whose values automap to Google Adsense ;)
Cieplakover 10 years ago
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.
评论 #8744820 未加载
anentropicover 10 years ago
why do people keep insisting on inventing this ugly and pointless &#x27;solution&#x27;? too much Javascript?<p>just use `mydict.setdefault()` and `defaultdict` and give up on the whole attr access thing. it&#x27;s a dict
评论 #8746179 未加载
michaelmiorover 10 years ago
*whose values
评论 #8746606 未加载