attrdict and namedtuple moved to lib

--HG--
branch : aspn
This commit is contained in:
Andreas
2009-10-22 19:51:09 +00:00
parent 8b0250eca4
commit 9fde793560

View File

@@ -5,11 +5,25 @@
# http://code.activestate.com/recipes/473786/
# http://groups.google.com/group/comp.lang.python/browse_thread/thread/fa88bba7184d9431
class AttrDict(dict):
"""A dict whose items can also be accessed as member variables.
"""
Looks fine as long as nobody uses an existing method name as a dictionary key:
A dictionary with attribute-style access. It maps attribute access to
the real dictionary. """
py> d = AttrDict({'name':'value'})
py> d.items()
[('name', 'value')]
py> d = AttrDict({'items': [1,2,3]})
py> d.items()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
(I should have included a test case for this issue too).
Of course, if this doesn't matter in your application, go ahead and use it. Just be aware of the problem.
"""
class AttrDict(dict):
"""A dict whose items can also be accessed as member variables."""
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
self.__dict__ = self