attrdict and namedtuple moved to lib
--HG-- branch : aspn
This commit is contained in:
@@ -5,11 +5,25 @@
|
|||||||
# http://code.activestate.com/recipes/473786/
|
# http://code.activestate.com/recipes/473786/
|
||||||
# http://groups.google.com/group/comp.lang.python/browse_thread/thread/fa88bba7184d9431
|
# 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
|
py> d = AttrDict({'name':'value'})
|
||||||
the real dictionary. """
|
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):
|
def __init__(self, *args, **kwargs):
|
||||||
dict.__init__(self, *args, **kwargs)
|
dict.__init__(self, *args, **kwargs)
|
||||||
self.__dict__ = self
|
self.__dict__ = self
|
||||||
|
|||||||
Reference in New Issue
Block a user